26. september 2004 - 00:42
Der er
4 kommentarer og 1 løsning
overskriv kun hvis .
Hejza allesammen.. Har fået denne dejlig kode af arne v.: void copy(WIN32_FIND_DATA *fnddat, char *fradir, char*tildir) { char fullfra[MAX_PATH],fulltil[MAX_PATH]; if(fnddat->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if(fnddat->cFileName[0]!='.') { sprintf(fullfra,"%s%s\\",fradir,fnddat->cFileName); sprintf(fulltil,"%s%s\\",tildir,fnddat->cFileName); CreateDirectory(fulltil,NULL); copy_recursive(fullfra,fulltil); } } else { sprintf(fullfra,"%s%s",fradir,fnddat->cFileName); sprintf(fulltil,"%s%s",tildir,fnddat->cFileName); CopyFile(fullfra,fulltil,1); } } void copy_recursive(char *fradir, char*tildir) { WIN32_FIND_DATA data; char spec[MAX_PATH]; sprintf(spec,"%s*.*",fradir); HANDLE h = FindFirstFile(spec,&data); if(h!=INVALID_HANDLE_VALUE) { copy(&data,fradir,tildir); while(FindNextFile(h,&data)) { copy(&data,fradir,tildir); } } FindClose(h); } copy_recursive("C:\\tester\\mappe1\\","C:\\tester\\mappe2"); Men vil jeg gerne gøre sådan at den kun må overskrive en fil, hvis redigeres datoen på den fil som overskriver er nyest ! Altså hvis der i mappe1 ligger en fil med redigeres dato 22-08-04 og tid : 22:00:00. Og der i mappe2 findes en fil med samme navn som her datoen 23-08-04 og tiden 22:30:00 skal den ikke overskrive. På forhånd tak.
Annonceindlæg fra Publicis Sapient
26. september 2004 - 00:52
#1
#include <windows.h> #include <stdlib.h> #include <string> #include <iostream> void DoCopy(std::string aSrc, std::string aDest, FILETIME &aSrcFileTime) { WIN32_FIND_DATA FindFileData; HANDLE hFind = FindFirstFile(aDest.c_str(), &FindFileData); if(hFind != INVALID_HANDLE_VALUE && CompareFileTime(&aSrcFileTime, &FindFileData.ftLastWriteTime) < 0) { std::cout << "Dest is newer than source: " << aSrc << std::endl; return; } if(!CopyFile(aSrc.c_str(), aDest.c_str(), false)) std::cerr << "Failed to copy: " << aSrc << std::endl; else std::cout << aSrc << std::endl; } void DoScan(std::string aSrc, std::string aDest) { std::string SrcPattern = aSrc + "*"; WIN32_FIND_DATA FindFileData; HANDLE hFind = FindFirstFile(SrcPattern.c_str(), &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { std::cerr << "Invalid File Handle. GetLastError reports: " << GetLastError() << std::endl; return; } else { CreateDirectory(aDest.c_str(), 0); do { if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { DoCopy(aSrc + FindFileData.cFileName, aDest + FindFileData.cFileName, FindFileData.ftLastWriteTime); } else if(strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) DoScan(aSrc + std::string(FindFileData.cFileName) + "\\", aDest + std::string(FindFileData.cFileName) + "\\"); } while(FindNextFile(hFind, &FindFileData)); FindClose(hFind); } } int main(void) { DoScan("D:\\temp1\\", "D:\\temp2\\"); return EXIT_SUCCESS; }
26. september 2004 - 00:59
#2
Takker alt for meget.. vil du stadig ikke have points ?
26. september 2004 - 01:02
#3
Nej tak. Jeg har holdt mig pointfri i et helt år nu, lad mig ikke blive fristet ;-)
26. september 2004 - 01:03
#4
hehe okay fair nok... Tusind tak...
26. september 2004 - 17:03
#5
Der kommer lige en forbedret DoCopy. Den lukker handlen og undlader at kopiere hvis filerne er fra samme tid: void DoCopy(std::string aSrc, std::string aDest, FILETIME &aSrcFileTime) { WIN32_FIND_DATA FindFileData; HANDLE hFind = FindFirstFile(aDest.c_str(), &FindFileData); if(hFind != INVALID_HANDLE_VALUE) { if(CompareFileTime(&aSrcFileTime, &FindFileData.ftLastWriteTime) <= 0) { std::cout << "Dest is newer than source: " << aSrc << std::endl; FindClose(hFind); return; } FindClose(hFind); } if(!CopyFile(aSrc.c_str(), aDest.c_str(), false)) std::cerr << "Failed to copy: " << aSrc << std::endl; else std::cout << aSrc << std::endl; }
Kurser inden for grundlæggende programmering