CString strDirectory;//项目文件所在目录
char cDataPath[MAX_PATH];//文件路径GetModuleFileName( NULL, cDataPath, MAX_PATH );
strDirectory=cDataPath;//将字符数组赋值过去
int k=strDirectory.ReverseFind('\\');//从后面查找\字符
strDirectory=strDirectory.Left(strDirectory.GetLength()-k);//截断获得\前面的一段字符串,即为目录文件夹
下面删除该文件夹下所有后缀名为.tmp的文件:
system(strDirectory); //跳转到该路径
system("del *.tmp"); //删除该路径下所有.tmp
因为调用了dos命令,因此会出现dos窗口闪烁的情况,那么可采用下面的方法:
<pre name="code" class="cpp">fcloseall();
CString strFind,strFindResult;
strFind=strDirectory+"\*.tmp";
CFileFind ff;
BOOL res = ff.FindFile(strFind);while(res)
{ strFindResult = ff.GetFileName();<pre name="code" class="cpp"> remove(strFindResult ); res = ff.FindNextFile();}
ff.Close();
本文介绍如何使用C++代码批量删除指定目录下所有后缀名为.tmp的文件,并提供了一个替代方法来避免出现DOS窗口闪烁的问题。通过解析目录路径并执行删除操作,实现自动化文件清理。
573

被折叠的 条评论
为什么被折叠?



