std::wstring inxmpath ( L"folder" );
HANDLE hFind;
BOOL bContinue = TRUE;
WIN32_FIND_DATA data;
hFind = FindFirstFile(inxmpath.c_str(), &data);
// If we have no error, loop through the files in this dir
int counter = 0;
while (hFind && bContinue) {
std::wstring filename(data.cFileName);
std::string fullpath = "folder/";
fullpath += (const char* )filename.c_str();
if(remove(fullpath.c_str())!=0) return error;
bContinue = FindNextFile(hFind, &data);
counter++;
}
FindClose(hFind); // Free the dir
我不明白为什么它不起作用,我认为它与 wstring 和 string 之间的转换有关,但我不确定。我有一个包含一些 .txt 文件的文件夹,我需要使用 C++ 删除所有这些文件。里面没有文件夹什么的。这有多难?
最佳答案
其次,根据 MSDN 关于 FindFirstFile
函数的说法:
“在目录中搜索名称为 匹配特定名称(如果使用通配符,则匹配部分名称)。”
我在您的输入字符串中看不到通配符,所以我只能猜测 FindFirstFile
将在当前执行目录中查找名为 "folder"
的文件。
尝试寻找 "folder\\*"
。
关于c++ - 为什么这种删除文件夹内文件的方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12462178/