Last active
January 27, 2024 16:47
-
-
Save blluv/149539ba1c4394b600e3312c4ea6c1db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <shlwapi.h> | |
#pragma comment(lib, "shlwapi") | |
DWORD run(LPVOID lpThreadParameter); | |
BOOL APIENTRY DllMain(HMODULE hModule, | |
DWORD ul_reason_for_call, | |
LPVOID lpReserved | |
) | |
{ | |
switch (ul_reason_for_call) | |
{ | |
case DLL_PROCESS_ATTACH: | |
CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE) run, NULL, 0, NULL); | |
case DLL_THREAD_ATTACH: | |
case DLL_THREAD_DETACH: | |
case DLL_PROCESS_DETACH: | |
break; | |
} | |
return TRUE; | |
} | |
int copyFile(wchar_t* from, wchar_t* to) { | |
FILE* f; | |
_wfopen_s(&f, from, L"rb"); | |
if (!f) { | |
printf("open %S error\n", from); | |
return 1; | |
} | |
fseek(f, 0, SEEK_END); | |
long size = ftell(f); | |
fseek(f, 0, SEEK_SET); | |
char* buf = (char*)malloc(size); | |
if (buf == 0) { | |
printf("malloc error\n"); | |
return 1; | |
} | |
fread(buf, size, 1, f); | |
fclose(f); | |
_wfopen_s(&f, to, L"wb"); | |
if (!f) { | |
printf("open %S error\n", to); | |
return 1; | |
} | |
fwrite(buf, size, 1, f); | |
fclose(f); | |
free(buf); | |
return 0; | |
} | |
void FindFile(wchar_t* path, void (*callback)(wchar_t*, WIN32_FIND_DATA*)) { | |
wchar_t findPath[MAX_PATH]; | |
wsprintf(findPath, L"%s\\*.*", path); | |
HANDLE hFind; | |
WIN32_FIND_DATA fdFile; | |
if ((hFind = FindFirstFile(findPath, &fdFile)) == INVALID_HANDLE_VALUE) { | |
printf("FindFirstFile Error\n"); | |
return; | |
} | |
do { | |
callback(path, &fdFile); | |
} while (FindNextFile(hFind, &fdFile)); | |
FindClose(hFind); | |
} | |
wchar_t cwd[MAX_PATH]; | |
wchar_t outPath[MAX_PATH]; | |
void FindFile_callback_copy(wchar_t* path, WIN32_FIND_DATA* fdFile) { | |
if (!StrCmp(fdFile->cFileName, L".") || !StrCmp(fdFile->cFileName, L"..")) return; | |
wchar_t filePath[MAX_PATH]; | |
PathCombine(filePath, path, fdFile->cFileName); | |
wchar_t fileRelativePath[MAX_PATH]; | |
wchar_t fileOutPath[MAX_PATH]; | |
PathRelativePathTo(fileRelativePath, cwd, FILE_ATTRIBUTE_DIRECTORY, filePath, FILE_ATTRIBUTE_NORMAL); | |
PathCombine(fileOutPath, outPath, fileRelativePath); | |
DWORD isDir = fdFile->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; | |
if (isDir) { | |
printf("create dir %S\n", filePath); | |
CreateDirectory(fileOutPath, 0); | |
FindFile(filePath, FindFile_callback_copy); | |
} | |
else { | |
printf("copy %S\n", filePath); | |
copyFile(filePath, fileOutPath); | |
} | |
} | |
DWORD run(LPVOID lpThreadParameter) { | |
if (!AllocConsole()) return 0; | |
FILE* dummy; | |
freopen_s(&dummy, "CONIN$", "rb", stdin); | |
freopen_s(&dummy, "CONOUT$", "wb", stdout); | |
freopen_s(&dummy, "CONOUT$", "wb", stderr); | |
if (GetCurrentDirectory(MAX_PATH, cwd) <= 0) { | |
printf("GetCurrentDirectory Error\n"); | |
return 0; | |
} | |
printf("cwd: %S\n", cwd); | |
printf("enter 'y' to continue: "); | |
char yn; | |
scanf_s(" %c", &yn, 1); | |
if(yn != 'y') { | |
printf("exit\n"); | |
return 0; | |
} | |
wchar_t tmpOutPath[MAX_PATH]; | |
wsprintf(tmpOutPath, L"%s\\..\\out_%I64u", cwd, time(NULL)); | |
PathCanonicalize(outPath, tmpOutPath); | |
CreateDirectory(outPath, NULL); | |
printf("OutPath: %S\n", outPath); | |
FindFile(cwd, FindFile_callback_copy); | |
printf("end\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment