I want to recursively scan a directory and all its sub-directories for files with a given extension - for example, all *.jpg files. How can you do that in Qt?
我想递归地扫描一个目录和它的所有子目录,找到一个给定扩展名的文件——例如,所有的*.jpg文件。如何在Qt中做到这一点?
3 个解决方案
#1
70
I suggest you give a look at QDirIterator.
我建议你看看QDirIterator。
QDirIterator it(dir, QStringList() << "*.jpg", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
qDebug() << it.next();
This makes the algorithm more scalable. QDir::entryList()
might take suddenly large amount of memory when many files are stored in a single directory. Not good on small embedded devices.
这使得算法更具有可扩展性。当许多文件存储在一个目录中时,entryList()可能会突然占用大量内存。不适合小型嵌入式设备。
#2
5
This should work :
这应该工作:
void scanDir(QDir dir)
{
dir.setNameFilters(QStringList("*.nut"));
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
qDebug() << "Scanning: " << dir.path();
QStringList fileList = dir.entryList();
for (int i=0; i<fileList.count(); i++)
{
if(fileList[i] != "main.nut" &&
fileList[i] != "info.nut")
{
qDebug() << "Found file: " << fileList[i];
}
}
dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
QStringList dirList = dir.entryList();
for (int i=0; i<dirList.size(); ++i)
{
QString newPath = QString("%1/%2").arg(dir.absolutePath()).arg(dirList.at(i));
scanDir(QDir(newPath));
}
}
The differences from your code are the following:
代码的不同之处如下:
- Breadth first search instead of depth first search (no reason for it, I just prefer it)
- 广度优先搜索而不是深度优先搜索(没有理由,我只是喜欢它)
- More filters in order to avoid sym links
- 更多的过滤器,以避免sym链接
- EntryList instead of EntryInfoList. You don t need if you just want the name of the file.
- EntryList代替EntryInfoList。如果只想要文件的名称,则不需要。
I tested it and it works correctly, but notice the following:
我对它进行了测试,结果是正确的,但是请注意以下内容:
- This may take a lot of time, so consider running it from thread
- 这可能需要很多时间,所以考虑从线程中运行它
- If there is deep recursion you may have problem with your stack
- 如果存在深度递归,您的堆栈可能有问题
#3
3
I used QDirIterator.
我用QDirIterator。
Here's how I do it and how simple it was to find all XML absolute file paths recursively very fast (Qt4.8.1):
下面是我的操作方法,简单地发现所有XML绝对文件路径递归非常快(Qt4.8.1):
// used to store the file paths
filesStack = new QStack<QString>();
// I use a file dialog to let the user choose the root folder to search in
if (fileDialog->exec() == QFileDialog::Accepted) {
QDir selectedDir(fileDialog->selectedFiles().first());
selectedDir.setFilter(QDir::Files |
QDir::Dirs | QDir::NoDot | QDir::NoDotDot);
QStringList qsl; qsl.append("*.xml"); // I only want XML files
selectedDir.setNameFilters(qsl);
findFilesRecursively(selectedDir);
}
// this function stores the absolute paths of each file in a QVector
void findFilesRecursively(QDir rootDir) {
QDirIterator it(rootDir, QDirIterator::Subdirectories);
while(it.hasNext()) {
filesStack->push(it.next());
}
}
Thanks to everyone for the hints.
谢谢大家的提示。
EDIT: I may have omitted some declarations, beware.
编辑:我可能漏掉了一些声明,小心。
#1
70
I suggest you give a look at QDirIterator.
我建议你看看QDirIterator。
QDirIterator it(dir, QStringList() << "*.jpg", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
qDebug() << it.next();
This makes the algorithm more scalable. QDir::entryList()
might take suddenly large amount of memory when many files are stored in a single directory. Not good on small embedded devices.
这使得算法更具有可扩展性。当许多文件存储在一个目录中时,entryList()可能会突然占用大量内存。不适合小型嵌入式设备。
#2
5
This should work :
这应该工作:
void scanDir(QDir dir)
{
dir.setNameFilters(QStringList("*.nut"));
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
qDebug() << "Scanning: " << dir.path();
QStringList fileList = dir.entryList();
for (int i=0; i<fileList.count(); i++)
{
if(fileList[i] != "main.nut" &&
fileList[i] != "info.nut")
{
qDebug() << "Found file: " << fileList[i];
}
}
dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
QStringList dirList = dir.entryList();
for (int i=0; i<dirList.size(); ++i)
{
QString newPath = QString("%1/%2").arg(dir.absolutePath()).arg(dirList.at(i));
scanDir(QDir(newPath));
}
}
The differences from your code are the following:
代码的不同之处如下:
- Breadth first search instead of depth first search (no reason for it, I just prefer it)
- 广度优先搜索而不是深度优先搜索(没有理由,我只是喜欢它)
- More filters in order to avoid sym links
- 更多的过滤器,以避免sym链接
- EntryList instead of EntryInfoList. You don t need if you just want the name of the file.
- EntryList代替EntryInfoList。如果只想要文件的名称,则不需要。
I tested it and it works correctly, but notice the following:
我对它进行了测试,结果是正确的,但是请注意以下内容:
- This may take a lot of time, so consider running it from thread
- 这可能需要很多时间,所以考虑从线程中运行它
- If there is deep recursion you may have problem with your stack
- 如果存在深度递归,您的堆栈可能有问题
#3
3
I used QDirIterator.
我用QDirIterator。
Here's how I do it and how simple it was to find all XML absolute file paths recursively very fast (Qt4.8.1):
下面是我的操作方法,简单地发现所有XML绝对文件路径递归非常快(Qt4.8.1):
// used to store the file paths
filesStack = new QStack<QString>();
// I use a file dialog to let the user choose the root folder to search in
if (fileDialog->exec() == QFileDialog::Accepted) {
QDir selectedDir(fileDialog->selectedFiles().first());
selectedDir.setFilter(QDir::Files |
QDir::Dirs | QDir::NoDot | QDir::NoDotDot);
QStringList qsl; qsl.append("*.xml"); // I only want XML files
selectedDir.setNameFilters(qsl);
findFilesRecursively(selectedDir);
}
// this function stores the absolute paths of each file in a QVector
void findFilesRecursively(QDir rootDir) {
QDirIterator it(rootDir, QDirIterator::Subdirectories);
while(it.hasNext()) {
filesStack->push(it.next());
}
}
Thanks to everyone for the hints.
谢谢大家的提示。
EDIT: I may have omitted some declarations, beware.
编辑:我可能漏掉了一些声明,小心。