Qt删除指定文件夹里的所有文件(设置过滤)

bool AppClass::zsHelperClearCacheFile()

{
QDir dir(_zsHelperCachePath);
QFileInfoList fileList;
QFileInfo curFile;
if (!dir.exists()) { return false; }//文件不存,则返回false

fileList = dir.entryInfoList(QDir::Dirs | QDir::Files
| QDir::Readable | QDir::Writable
| QDir::Hidden | QDir::NoDotAndDotDot
, QDir::Name);
int filterCount = 0;
QString filterAvatarPath = _zsHelperCacheAvatarPath.replace("\\", "/");
QString filtermd5FilePath = _zsHelperCachePath.replace("\\", "/")+"/zsHelperMD5File.txt";
fileList.removeAll(QFileInfo(QFile(filtermd5FilePath)));//设置过滤
fileList.removeAll(QFileInfo(QDir(filterAvatarPath),"")); 
while (fileList.size()>0)//跳出条件
{
int infoNum = fileList.size();
for (int i = infoNum - 1; i >= 0; i--)
{
curFile = fileList[i];
if (curFile.isFile())//如果是文件,删除文件
{
QFile fileTemp(curFile.filePath());
fileTemp.remove();
fileList.removeAt(i);
}
if (curFile.isDir())//如果是文件夹
{
QDir dirTemp(curFile.filePath());
QFileInfoList fileList1 = dirTemp.entryInfoList(QDir::Dirs | QDir::Files
| QDir::Readable | QDir::Writable
| QDir::Hidden | QDir::NoDotAndDotDot
, QDir::Name);
if (fileList1.size() == 0)//下层没有文件或文件夹
{
dirTemp.rmdir(".");
fileList.removeAt(i);
}
else//下层有文件夹或文件
{
for (int j = 0; j<fileList1.size(); j++)
{
if (!(fileList.contains(fileList1[j])))
fileList.append(fileList1[j]);
}
}
}
}
}
return true;
}

 

备注:

该内容是转自http://blog.csdn.net/woaidongdongqu/article/details/7956994?_t_t_t=0.9721949512604624 然后进行修改

将所有要删除的文件加入一个集合中,然后在集合中过滤,最后剩下就是我们需要删除的内容

posted on 2017-04-20 17:56  阿兴的平凡世界  阅读(1950)  评论(0编辑  收藏  举报

导航