View Full Version : glob like fuction to list all files in subfolders that match wildcard.
enricong
24th December 2014, 00:36
Basically want something similar to the python glob command.
glob("*/abc*.dat")
would search the first level of subfolders and give me string list of file paths for all files (from cwd) starting with "abc" ending with ".dat"
Is there a quick similar QT function?
jefftee
24th December 2014, 08:17
Check out QDir, specifically QDir::entryList or QDir::entryInfoList, QDir::setNameFilters depending on your needs.
enricong
24th December 2014, 17:33
I tried that but unless I am doing something wrong, I can't get it to list files in subfolders
If my folder structure is:
folder1/abc1.txt
folder1/xyz1.txt
folder1/abc2.txt
folder2/abc3.txt
folder2/xyz4.txt
folder3/abc5.txt
then if I give "*/abc*"
I'm looking for an output of
folder1/abc1.txt
folder1/abc2.txt
folder2/abc3.txt
folder3/abc5.txt
I tried entrylist but it just lets me filter on whats in the current folder
jefftee
25th December 2014, 00:20
I tried that but unless I am doing something wrong, I can't get it to list files in subfolders
What filter(s) are you specifying for QDir::entryList or QDir::entryInfoList?
The entryInfoList and entryInfo methods only operate on the directory path represented by the QDir variable instance. If you want to process sub-directories, you need to ask to receive directory entries *and* files and recurse into the sub-directories as well.
I'd suggest something like this (I have not compiled or tested this, it's only from memory):
void process_dir(const QString &path)
{
QDir dir(path);
QStringList name_filters;
name_filters << "abc*";
QFileInfoList fil = dir.entryInfoList(name_filters, QDir::NoDotAndDotDot|QDir::AllDirs|QDir::Files);
for (int i = 0; i < fil.size(); i++)
{
QFileInfo fi = fil.at(i);
if (fi.isDir())
process_dir(fi.absolutePath());
if (fi.isFile())
do_something_with_file(fi);
}
return;
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.