删除目录中的所有文件

时间:2022-01-03 02:22:48

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,但没有运气。

7 个解决方案

#1


43  

Bjorns Answer tweeked to not loop forever

Bjorns回答说不要永远循环

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

#2


6  

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中,您可以在dirs上使用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()(这将完全删除它)。然后,在此之后使用相同的名称重新创建它...效果将是相同的,但代码行数较少。然而,这种更详细的功能提供了更多的可能性,如果需要,可以添加详细的异常处理,例如,检测特定文件/文件夹的访问冲突...

#3


5  

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”结尾的每个fileName调用QDir :: remove(fileName)来删除该文件。

#4


1  

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

你以一种很好的方式开始,看看entryList,当然还要传递你想要的名字过滤器。

#5


0  

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());
}

#6


0  

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);
}

#7


-3  

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将成为你的朋友。它易于使用,只需浏览手册页;)。

#1


43  

Bjorns Answer tweeked to not loop forever

Bjorns回答说不要永远循环

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

#2


6  

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中,您可以在dirs上使用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()(这将完全删除它)。然后,在此之后使用相同的名称重新创建它...效果将是相同的,但代码行数较少。然而,这种更详细的功能提供了更多的可能性,如果需要,可以添加详细的异常处理,例如,检测特定文件/文件夹的访问冲突...

#3


5  

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”结尾的每个fileName调用QDir :: remove(fileName)来删除该文件。

#4


1  

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

你以一种很好的方式开始,看看entryList,当然还要传递你想要的名字过滤器。

#5


0  

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());
}

#6


0  

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);
}

#7


-3  

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将成为你的朋友。它易于使用,只需浏览手册页;)。