在Windows下使用MFC/Win32 API进行文件夹内所有文件的复制操作,可以通过遍历目录中的文件并逐个复制来实现
#include <windows.h>
#include <Shlobj.h>
#include <wchar.h>
#include <iostream> // 用于控制台输出
// 函数声明
bool CopyFolder(const wchar_t* pszSrcDir, const wchar_t* pszDstDir);
int wmain()
{
//wchar_t szSrcDir[MAX_PATH];
//wchar_t szDstDir[MAX_PATH];
使用常量路径代替输入
//const wchar_t* pszSrcDir = L"E:\\soui\\LocationalTool\\Localization\\";
//const wchar_t* pszDstDir = L"E:\\soui\\LocationalTool\\dest\\";
//if (CopyFolder(pszSrcDir, pszDstDir))
// wprintf(L"Directory copied successfully.\n");
//else
// wprintf(L"Failed to copy directory.\n");
//return 0;
const wchar_t* pszSrcFile = L"E:\\soui\\LocationalTool\\Localization\\Game\\en\\Game.locres";
const wchar_t* pszDstDir = L"E:\\soui\\LocationalTool\\dest\\";
wchar_t szDstFile[MAX_PATH];
wcscpy_s(szDstFile, pszDstDir);
wcscat_s(szDstFile, L"Game.locres");
if (CopyFileW(pszSrcFile, szDstFile, FALSE))
wprintf(L"File copied successfully.\n");
else
{
DWORD dwError = GetLastError();
wprintf(L"Failed to copy file. Error: %lu\n", dwError);
}
return 0;
}
bool CopyFolder(const wchar_t* pszSrcDir, const wchar_t* pszDstDir)
{
WIN32_FIND_DATA findData;
HANDLE hFind = INVALID_HANDLE_VALUE;
wchar_t szFullSrcPath[MAX_PATH];
wchar_t szFullDstPath[MAX_PATH];
// 构建通配符搜索字符串
wcscpy_s(szFullSrcPath, pszSrcDir);
wcscat_s(szFullSrcPath, L"*");
// 检查源目录是否存在
DWORD dwAttrib = GetFileAttributesW(pszSrcDir);
if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
DWORD dwError = GetLastError();
wprintf(L"Failed to get attributes for '%ls'. Error: %lu\n", pszSrcDir, dwError);
return false;
}
if (!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
wprintf(L"Source path '%ls' is not a directory.\n", pszSrcDir);
return false;
}
// 创建目标目录
if (!::CreateDirectoryW(pszDstDir, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
DWORD dwError = GetLastError();
wprintf(L"Failed to create destination directory '%ls'. Error: %lu\n", pszDstDir, dwError);
return false;
}
// 查找第一个文件
hFind = FindFirstFile(szFullSrcPath, &findData);
if (hFind == INVALID_HANDLE_VALUE) {
DWORD dwError = GetLastError();
wprintf(L"Failed to find files in '%ls'. Error: %lu\n", pszSrcDir, dwError);
return false;
}
do {
// 跳过"." 和 ".." 目录
if (wcscmp(findData.cFileName, L".") != 0 &&
wcscmp(findData.cFileName, L"..") != 0) {
wprintf(L"Processing '%ls'\n", findData.cFileName); // 输出正在处理的文件/目录
wcscpy_s(szFullSrcPath, pszSrcDir);
wcscat_s(szFullSrcPath, L"\\");
wcscat_s(szFullSrcPath, findData.cFileName);
wcscpy_s(szFullDstPath, pszDstDir);
wcscat_s(szFullDstPath, L"\\");
wcscat_s(szFullDstPath, findData.cFileName);
// 如果是目录,则递归复制
if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
wprintf(L"Recursive call for directory '%ls'\n", szFullSrcPath); // 输出递归调用信息
if (!CopyFolder(szFullSrcPath, szFullDstPath))
return false;
}
else { // 否则复制文件
SHFILEOPSTRUCT fileOp = { 0 };
fileOp.wFunc = FO_COPY;
fileOp.pFrom = szFullSrcPath;
fileOp.pTo = szFullDstPath;
fileOp.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;
if (!SHFileOperation(&fileOp)) {
DWORD dwError = GetLastError();
wprintf(L"Failed to copy file '%ls' to '%ls'. Error: %lu\n", szFullSrcPath, szFullDstPath, dwError);
return false;
}
}
}
} while (FindNextFile(hFind, &findData));
FindClose(hFind);
return true;
}