[英]Copy all files .doc or .docx in folder and subfolder into another folder
I am new to C++ and winapi, currently working on a project to create a winapi application with a function to copy all files.doc and.docx in one drive to another folder. I am new to C++ and winapi, currently working on a project to create a winapi application with a function to copy all files.doc and.docx in one drive to another folder. Below is what I have done and it doesn't seem to work:
以下是我所做的,它似乎不起作用:
Can anyone show me how to do this properly?谁能告诉我如何正确地做到这一点?
void cc(wstring inputstr) {
TCHAR sizeDir[MAX_PATH];
wstring search = inputstr + TEXT("\\*");
wcscpy_s(sizeDir, MAX_PATH, search.c_str());
WIN32_FIND_DATA findfiledata;
HANDLE Find = FindFirstFile(sizeDir, &findfiledata);
do {
if (findfiledata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (!wcscmp(findfiledata.cFileName, TEXT(".")) || !wcscmp(findfiledata.cFileName, TEXT(".."))) continue;
//checking folder or file
wstring dirfolder = inputstr + TEXT("\\") + findfiledata.cFileName;
cc(dirfolder);
}
else {
wstring FileSearch = findfiledata.cFileName;
//.doc or docx
if (!wcscmp(FileSearch.c_str(), L".doc") || !wcscmp(FileSearch.c_str(), L".docx")) {
TCHAR src[256] = L"D:\\test\\";
wstring dirsrc = inputstr + TEXT("\\") + findfiledata.cFileName;
_tprintf(TEXT(" %s \n"), dirsrc.c_str());
wcscat_s(src, findfiledata.cFileName);
CopyFile(dirsrc.c_str(), src, TRUE);
}
}
} while (FindNextFile(Find, &findfiledata) != 0);
FindClose(Find);
}
The inputstr
here when i call the function is the drive that i want to search like cc(L"D:");
当我调用 function 时,此处的
inputstr
是我想要搜索的驱动器,例如cc(L"D:");
if (!wcscmp(FileSearch.c_str(), L".doc") || !wcscmp(FileSearch.c_str(), L".docx"))
This is comparing the whole file name.这是比较整个文件名。 You only want to compare the file extension:
您只想比较文件扩展名:
...
else
{
std::wstring test = findfiledata.cFileName;
auto dot = test.find_last_of(L'.');
if (dot == std::wstring::npos)
continue;
auto ext = test.substr(dot);
if (ext == L".doc" || ext == L".docx")
{
std::wstring path = inputstr + L"\\" + findfiledata.cFileName;
std::wcout << path << '\n';
//vec.push_back(path) instead of CopyFile!
}
}
Putting CopyFile
inside that recursive function may cause problems.将
CopyFile
放入该递归 function 可能会导致问题。 FindNextFile
could see the new copied file, and the function tries to copy it again. FindNextFile
可以看到新复制的文件,并且 function 尝试再次复制它。
You could instead save the result in a vector of strings, then copy the file once cc
is finished.您可以改为将结果保存在字符串向量中,然后在
cc
完成后复制文件。
void cc(std::wstring inputstr, std::vector<std::wstring> &vec);
...
std::vector<std::wstring> vec;
cc(L"D:", vec);
for (auto& e : vec)
std::wcout << e << '\n';
Also initialize findfiledata
to zero findfiledata
初始化为零
WIN32_FIND_DATA findfiledata = { 0 };
You must use wcsstr
or std::wstring::find
instead of wcscmp
because you want to search string .doc
inside FileSearch
, not comparing FileSearch
with string .doc
.您必须使用
wcsstr
或std::wstring::find
而不是wcscmp
,因为您想在FileSearch
中搜索字符串.doc
,而不是将FileSearch
与字符串.doc
进行比较。 I re-write your else
branch:我重写了你的
else
分支:
else {
wstring FileSearch = findfiledata.cFileName;
std::transform(FileSearch.begin(), FileSearch.end(), FileSearch.begin(), ::towlower);
//.doc or docx
if (wcsstr(FileSearch.c_str(), L".doc") || wcsstr(FileSearch.c_str(), L".docx"))
{
TCHAR src[256] = L"D:\\test\\";
wstring dirsrc = inputstr + TEXT("\\") + findfiledata.cFileName;
wprintf_s(TEXT(" %s \n"), dirsrc.c_str());
wcscat_s(src, findfiledata.cFileName);
CopyFile(dirsrc.c_str(), src, TRUE);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.