删除在C中找到的文件:\ 我有这个C++源来查找 *.do/文件>代码>:\ < /p>
我的问题是,我不想显示它们,而是想删除它们 如何删除所有删除在C中找到的文件:\ 我有这个C++源来查找 *.do/文件>代码>:\ < /p>,c++,C++,我的问题是,我不想显示它们,而是想删除它们 如何删除所有*。c:\中的文件?我的代码只显示目录路径,我想改为删除 #include <windows.h> #include <string> #include <iostream> void GetFileListing(std::string directory, std::string fileFilter, bool recursively = true) { if (recursively)
*。c:\
中的文件?我的代码只显示目录路径,我想改为删除
#include <windows.h>
#include <string>
#include <iostream>
void GetFileListing(std::string directory, std::string fileFilter, bool recursively = true)
{
if (recursively)
GetFileListing(directory, fileFilter, false);
directory += "\\";
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
std::string filter = directory + (recursively ? "*" : fileFilter);
hFind = FindFirstFile(filter.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return;
}
else
{
if (!recursively)
{
std::cout << directory + std::string(FindFileData.cFileName) << std::endl;
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if (!recursively)
{
std::cout << directory + std::string(FindFileData.cFileName) << std::endl;
}
else
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
{
GetFileListing(directory + std::string(FindFileData.cFileName), fileFilter);
}
}
}
DWORD dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
std::cout << "FindNextFile error. Error is "<< dwError << std::endl;
}
}
}
int main(int argc, char* argv[])
{
GetFileListing("c:\\", "*.do");
}
#包括
#包括
#包括
void GetFileListing(std::string目录,std::string文件过滤器,bool递归=true)
{
if(递归地)
GetFileListing(目录,文件过滤器,false);
目录+=“\\”;
WIN32_FIND_DATA FindFileData;
句柄hFind=无效的句柄值;
std::string filter=directory+(递归?*”:fileFilter);
hFind=FindFirstFile(filter.c_str(),&FindFileData);
if(hFind==无效的句柄值)
{
回来
}
其他的
{
if(!递归地)
{
std::cout制作一些(可能是std::list
,可能是std::set
或std::vector
)包含找到的(绝对)文件路径集合,然后删除所有这些路径(使用或仅使用C…)
因此,您有两个步骤:首先,您在文件系统上迭代以查找相关文件(在Linux上,我会使用它;也许您的操作系统有类似的东西),而不删除它们(但在容器中收集它们的完整文件路径)。其次,您在容器上迭代以删除其中的每个文件路径。您正在查找的文件,例如:
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“shlwapi.lib”);
void getFolderList(std::string目录、const std::string和fileFilter、std::vector*子文件夹、std::vector*文件)
{
如果(!directory.empty()&&(*directory.rbegin()!='\\'))
目录+=“\\”;
std::string filter=目录;
如果(子文件夹)
过滤器+=“*”;
其他的
filter+=fileFilter;
WIN32_FIND_DATAA FindFileData;
处理高频风;
德沃德误差;
hFind=FindFirstFileA(filter.c_str(),&FindFileData);
if(hFind==无效的句柄值)
{
dwError=GetLastError();
if(dwError!=未找到错误文件)
STD:C++ C。你的问题是关于一个或另一个。它们不是同一种语言,标签不是同义词。这里标签是相关的。只使用那些实际适用于你的问题的标签。他的问题是关于C++。主题中是一个磁盘,而不是一种语言。谢谢你的回答。你能给我一个例子吗?是这样的吗?std::删除否,构建完整的绝对文件路径,并将其添加到容器中,然后重复下一个找到的文件。哦,这看起来很神奇!但是给我一些错误:第21行'cout'不是'std'行21'endl'i的成员“继续”之前的“标准”第57行“cout”不是“标准”第57行的成员“endl”不是“标准”第64行的成员“iter”的名称查找更改为“ISO”的“范围[-fppermissive]std
错误是因为我最初删除了std::cout
调用,然后删除了#include
,然后又重新添加了std::cout
,但忘记了重新添加#include
。我已经修复了。谢谢!现在它只显示了一个错误:(.text+0x1f5):未定义对`\u imp的引用__PathMatchSpecA@8“我做错了什么?@RickJoe:那应该由#pragma
来处理。也许你的编译器的shlwapi.lib
没有引用PathMatchSpecA()
。你真的不应该使用Ansi API(我之所以这样做,是因为您使用的是std::string
),您应该改用Unicode API。使用\*
过滤器调用FindFirstFile()
会匹配
中的所有内容,因此您必须手动比较文件扩展名。为您处理:“使用Microsoft MS-DOS通配符匹配类型搜索字符串。”如果无法生成PathMatchSpec()
工作时,您必须找到另一个要使用的模式匹配函数,可能来自正则表达式库,或者编写自己的比较代码,从cFileName
中提取文件扩展名,并手动将其与fileFilter
进行比较。
#include <windows.h>
#include <Shlwapi.h>
#include <iostream>
#include <string>
#include <vector>
#pragma comment(lib, "shlwapi.lib");
void GetFolderListing(std::string directory, const std::string &fileFilter, std::vector<std::string> *subfolders, std::vector<std::string> *files)
{
if (!directory.empty() && (*directory.rbegin() != '\\'))
directory += "\\";
std::string filter = directory;
if (subfolders)
filter += "*";
else
filter += fileFilter;
WIN32_FIND_DATAA FindFileData;
HANDLE hFind;
DWORD dwError;
hFind = FindFirstFileA(filter.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
if (dwError != ERROR_FILE_NOT_FOUND)
std::cout << "FindFirstFile error " << dwError << std::endl;
return;
}
do
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((subfolders) &&
(lstrcmpA(FindFileData.cFileName, ".") != 0) &&
(lstrcmpA(FindFileData.cFileName, "..") != 0))
{
subfolders->push_back(directory + FindFileData.cFileName);
}
}
else
{
if ((files) &&
((!subfolders) || PathMatchSpecA(FindFileData.cFileName, fileFilter.c_str())))
{
files->push_back(directory + FindFileData.cFileName);
}
}
}
while (FindNextFileA(hFind, &FindFileData));
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
std::cout << "FindNextFile error " << dwError << std::endl;
}
void DeleteFiles(const std::string &directory, const std::string &fileFilter, bool recursively = true)
{
std::vector<std::string> folders;
std::vector<std::string> files;
GetFolderListing(directory, fileFilter, (recursively) ? &folders : NULL, &files);
for (std::vector<std::string>::iterator iter = files.begin(); iter != files.end(); ++iter)
DeleteFileA(iter->c_str());
if (recursively)
{
for (std::vector<std::string>::iterator iter = folders.begin(); iter != folders.end(); ++iter)
DeleteFiles(*iter, fileFilter, true);
}
}
int main(int argc, char* argv[])
{
DeleteFiles("c:\\", "*.do");
}
#include <windows.h>
#include <Shlwapi.h>
#include <iostream>
#include <string>
#include <vector>
#pragma comment(lib, "shlwapi.lib");
void GetFolderListing(std::wstring directory, const std::wstring &fileFilter, std::vector<std::wstring> *subfolders, std::vector<std::wstring> *files)
{
if (!directory.empty() && (*directory.rbegin() != L'\\'))
directory += L"\\";
std::wstring filter = directory;
if (subfolders)
filter += L"*";
else
filter += fileFilter;
WIN32_FIND_DATAW FindFileData;
HANDLE hFind;
DWORD dwError;
hFind = FindFirstFileW(filter.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
if (dwError != ERROR_FILE_NOT_FOUND)
std::cout << "FindFirstFile error " << dwError << std::endl;
return;
}
do
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((subfolders) &&
(lstrcmpW(FindFileData.cFileName, L".") != 0) &&
(lstrcmpW(FindFileData.cFileName, L"..") != 0))
{
subfolders->push_back(directory + FindFileData.cFileName);
}
}
else
{
if ((files) &&
((!subfolders) || PathMatchSpecW(FindFileData.cFileName, fileFilter.c_str())))
{
files->push_back(directory + FindFileData.cFileName);
}
}
}
while (FindNextFileW(hFind, &FindFileData));
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
std::cout << "FindNextFile error " << dwError << std::endl;
}
void DeleteFiles(const std::wstring &directory, const std::wstring &fileFilter, bool recursively = true)
{
std::vector<std::wstring> folders;
std::vector<std::wstring> files;
GetFolderListing(directory, fileFilter, (recursively) ? &folders : NULL, &files);
for (std::vector<std::wstring>::iterator iter = files.begin(); iter != files.end(); ++iter)
DeleteFileW(iter->c_str());
if (recursively)
{
for (std::vector<std::wstring>::iterator iter = folders.begin(); iter != folders.end(); ++iter)
DeleteFiles(*iter, fileFilter, true);
}
}
int wmain(int argc, wchar_t* argv[])
{
DeleteFiles(L"c:\\", L"*.do");
}