Delete only one file from Folder Documents
Solved
General and Desktop
-
Hi,
is it possible to delete only one file from Documents with QDir? For example the file test.htm?
I only heared about deleting all Content in a Folder but not only one file.Thank you,
Henrik -
@HenrikSt.
Have you tried remove(const QString & fileName) ? -
Hi,
Don't believe what you hear, just read the excellent docs :)
bool QDir::remove(const QString &fileName) -
After your request in chat, here is a small sample how you can do this:
QDir dir(pathYouWantToSearch); QDirIterator it(dir); while (it.hasNext()) { QString currentFilePath = it.next(); QFileInfo info(currentFilePath); // Or simply replace these 3 lines QString fileName = info.fileName(); // with 'if (currentFilePath.endsWith("test.html")) if (fileName == "test.htm") { // if you won't do anything more complex than that dir.remove(currentFilePath); break; } }
Edit: Maybe this is overkill if you always want to delete the same file from the same directory. Just use sth like this
QDir dir(dirYouWant); dir.remove(fullPathToFile);
And even simpler:
QDir().remove(fullPathToTestHtm);