Skip to content

Instantly share code, notes, and snippets.

@kevinlynx
Created October 16, 2013 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinlynx/7007074 to your computer and use it in GitHub Desktop.
Save kevinlynx/7007074 to your computer and use it in GitHub Desktop.
// e.g: copyDir("folder\\", "destFolder\\", false);
bool copyDir(const char *r_szSrcPath, const char *r_szDesPath, bool recur) {
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char l_szTmp[MAX_PATH];
strcpy(l_szTmp, r_szSrcPath);
char l_szSrcPath[MAX_PATH];
char l_szDesPath[MAX_PATH];
strcpy(l_szSrcPath, r_szSrcPath);
strcpy(l_szDesPath, r_szDesPath);
char l_szNewSrcPath[MAX_PATH];
char l_szNewDesPath[MAX_PATH];
strcat(l_szTmp, "*");
CreateDirectory(r_szDesPath, NULL);
hFind = FindFirstFile(l_szTmp, &FindFileData);
if (hFind == NULL) return false;
do {
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (recur &&
strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) {
sprintf(l_szNewDesPath,"%s%s\\", l_szDesPath, FindFileData.cFileName);
sprintf(l_szNewSrcPath,"%s%s\\", l_szSrcPath, FindFileData.cFileName);
copyDir(l_szNewSrcPath, l_szNewDesPath, true);
}
} else {
char l_szSrcFile[MAX_PATH];
char l_szDesFile[MAX_PATH];
sprintf(l_szDesFile, "%s%s", l_szDesPath, FindFileData.cFileName);
sprintf(l_szSrcFile, "%s%s", l_szSrcPath, FindFileData.cFileName);
CopyFile(l_szSrcFile, l_szDesFile, TRUE);
}
} while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment