C++ 删除目录中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14754977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:39:54  来源:igfitidea点击:

Delete all files in a directory

c++qt

提问by user1794021

I need to delete all files in a directory using Qt.

我需要使用 Qt 删除目录中的所有文件。

All of the files in the directory will have the extension ".txt".

目录中的所有文件都将具有扩展名“.txt”。

I don't want to delete the directory itself.

我不想删除目录本身。

Does anyone know how I can do this? I've looked at QDir but am having no luck.

有谁知道我怎么能做到这一点?我看过 QDir 但我没有运气。

回答by rreeves

Bjorns Answer tweeked to not loop forever

Bjorns 回答 tweeked 不会永远循环

QString path = "whatever";
QDir dir(path);
dir.setNameFilters(QStringList() << "*.*");
dir.setFilter(QDir::Files);
foreach(QString dirFile, dir.entryList())
{
    dir.remove(dirFile);
}

回答by user3191791

Ignoring the txt extension filtering... Here's a way to delete everything in the folder, including non-empty sub directories:

忽略txt扩展名过滤...这里有一个方法可以删除文件夹中的所有内容,包括非空子目录:

In QT5, you can use removeRecursively() on dirs. Unfortunately, that removes the whole directory - rather than just emptying it. Here is basic a function to just clear a directory's contents.

在 QT5 中,您可以在目录上使用 removeRecursively()。不幸的是,这会删除整个目录 - 而不仅仅是清空它。这是清除目录内容的基本功能。

void clearDir( const QString path )
{
    QDir dir( path );

    dir.setFilter( QDir::NoDotAndDotDot | QDir::Files );
    foreach( QString dirItem, dir.entryList() )
        dir.remove( dirItem );

    dir.setFilter( QDir::NoDotAndDotDot | QDir::Dirs );
    foreach( QString dirItem, dir.entryList() )
    {
        QDir subDir( dir.absoluteFilePath( dirItem ) );
        subDir.removeRecursively();
    }
}

Alternatively, you could use removeRecursively() on the directory you want to clear (which would remove it altogether). Then, recreate it with the same name after that... The effect would be the same, but with fewer lines of code. This more verbose function, however, provides more potential for detailed exception handling to be added if desired, e.g. detecting access violations on specific files / folders...

或者,您可以在要清除的目录上使用 removeRecursively() (这将完全删除它)。然后,在那之后用相同的名称重新创建它......效果是一样的,但代码行更少。然而,如果需要,这个更详细的功能为添加详细的异常处理提供了更多的可能性,例如检测特定文件/文件夹的访问冲突......

回答by Jeremy Friesner

Call QDir::entryList(QDir::Files) to get a list of all the files in the directory, and then for each fileName that ends in ".txt" call QDir::remove(fileName) to delete the file.

调用 QDir::entryList(QDir::Files) 获取目录中所有文件的列表,然后对于每个以“.txt”结尾的文件名调用 QDir::remove(fileName) 以删除文件。

回答by Zlatomir

You started in a good way, look at entryListand of course pass the namefilter you want.

您以一种很好的方式开始,查看entryList并当然传递您想要的名称过滤器。

回答by SergeyUr

Other variant of rreeves's code:

rreeves 代码的其他变体:

QDir dir("/path/to/file");

dir.setNameFilters(QStringList() << "*.*");
dir.setFilter(QDir::Files);

for(const QString &dirFile: dir.entryList()) {
  dir.remove(dirFile);
}

回答by Bjorn

This is how I would do it:

这就是我将如何做到的:

QString path = "name-of-directory";
QDir dir(path);
dir.setNameFilters(QStringList() << "*.txt");
dir.setFilters(QDir::Files);
while(dir.entryList().size() > 0){
    dir.remove(dir.entryList().first());
}

回答by SeedmanJ

You can achieve this without using Qt: to do so, opendir, readdir, unlink, and even rmdir will be your friends. It's easy to use, just browse the man pages ;).

你可以在不使用 Qt 的情况下实现这一点:这样做,opendir、readdir、unlink 甚至 rmdir 都将成为你的朋友。它易于使用,只需浏览手册页 ;)。