如果 dir 中有一些文件test.txt, test2.txt, test3.txt
,我可以使用 cmd 命令删除所有文件
del test*.txt
如何QT C++
在不使用的情况下做同样的事情system()
我试过了
QFile("test*.txt").remove();
但它不起作用。
主要任务是过滤文件,以便我们可以使用QDir
如下nameFilter
所示:
QDir dir("/path/of/directory", {"test*.txt"});
for(const QString & filename: dir.entryList()){
dir.remove(filename);
}
Run Code Online (Sandbox Code Playgroud)
或者使用 QDirIterator:
QDirIterator it("/path/of/directory", {"test*.txt"});
while (it.hasNext())
QFile(it.next()).remove();
//QDir().remove(it.next());
Run Code Online (Sandbox Code Playgroud)