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

Viewed 3005

If I have some files in dir test.txt, test2.txt, test3.txt, I can remove all of them by using cmd command

del test*.txt

How to do the same with QT C++ without using system()

I've tried

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

but it is not working.

1 Answers

The main task is to filter the files so we can use QDir with the nameFilter as shown below:

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

Or use QDirIterator:

QDirIterator it("/path/of/directory", {"test*.txt"});

while (it.hasNext())
    QFile(it.next()).remove();
    //QDir().remove(it.next());
Related