QT C++删除带有 *(名称包含)的文件

QT C++ remove file with * (name contains)

本文关键字:包含 文件 删除 C++ QT      更新时间:2023-10-16

如果我在目录test.txt, test2.txt, test3.txt中有一些文件,我可以使用 cmd 命令删除所有这些文件

del test*.txt

如何在不使用system()的情况下对QT C++执行相同的操作

我试过了

QFile("test*.txt").remove();

但它不起作用。

主要任务是过滤文件,以便我们可以将QDirnameFilter一起使用,如下所示:

QDir dir("/path/of/directory", {"test*.txt"});
for(const QString & filename: dir.entryList()){
dir.remove(filename);
}

或者使用 QDirIterator:

QDirIterator it("/path/of/directory", {"test*.txt"});
while (it.hasNext())
QFile(it.next()).remove();
//QDir().remove(it.next());
相关文章: