Recursively Iterate Over All the Files in a Directory and Its Subdirectories in Qt

Recursively iterate over all the files in a directory and its subdirectories in Qt

I suggest you have a look at QDirIterator.

QDirIterator it(dir, QStringList() << "*.jpg", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
qDebug() << it.next();

You could simply use QDir::entryList() recursively, but QDirIterator is simpler. Also, if you happen to have directories with a huge amount of files, you'd get pretty large lists from QDir::entryList(), which may not be good on small embedded devices.

Example (dir is QDir::currentPath()):

luca @ ~/it_test - [] $ tree
.
├── dir1
│   ├── image2.jpg
│   └── image3.jpg
├── dir2
│   └── image4.png
├── dir3
│   └── image5.jpg
└── image1.jpg

3 directories, 5 files
luca @ ~/it_test - [] $ /path/to/app
"/home/luca/it_test/image1.jpg"
"/home/luca/it_test/dir3/image5.jpg"
"/home/luca/it_test/dir1/image2.jpg"
"/home/luca/it_test/dir1/image3.jpg"

Walk a directory recursively in Qt, skip the folders . and ..

You should try to use the QDir::NoDotAndDotDot filter in your entryInfoList, as described in the documentation.

EDIT

  • Don't forget to add a QDir::Files, or QDir::Dirs or QDir::AllFiles to pick up the files and/or directories, as described in this post.

  • You may also want to check this previous question.

How do you iterate through every file/directory recursively in standard C++?

In standard C++, technically there is no way to do this since standard C++ has no conception of directories. If you want to expand your net a little bit, you might like to look at using Boost.FileSystem. This has been accepted for inclusion in TR2, so this gives you the best chance of keeping your implementation as close as possible to the standard.

An example, taken straight from the website:

bool find_file( const path & dir_path,         // in this directory,
const std::string & file_name, // search for this name,
path & path_found ) // placing path here if found
{
if ( !exists( dir_path ) ) return false;
directory_iterator end_itr; // default construction yields past-the-end
for ( directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
if ( is_directory(itr->status()) )
{
if ( find_file( itr->path(), file_name, path_found ) ) return true;
}
else if ( itr->leaf() == file_name ) // see below
{
path_found = itr->path();
return true;
}
}
return false;
}

Traversing a directory for specified folders? Qt & c++

Have a look at QDir::entryList(), which will list all the files in a folder. You can construct the directory path like so:

QString path = QDir::currentPath() + "/" +
QString::fromNumber(year) + "/" +
QString::fromNumber(month) + "/" +
QString::fromNumber(day);

and then do

QDir dir(path);
QStringList files = dir.entryList();

You can do the same for the other date and then combine the two QStringLists.

Recursively loop through directory and collect all xml files

You call it.next() twice in your loop. Thus you skip each second file. In other words you output each file path on odd position and collect each file path on even position.

while(it.hasNext()) {
qDebug() << "FILE" << it.next();
filepaths.append(it.next());
}

Should be something like below:

while(it.hasNext()) {
const auto& fn = it.next();
qDebug() << "FILE" << fn;
filepaths.append(fn);
}

Or:

while(it.hasNext()) {
qDebug() << "FILE" << it.next();
filepaths.append(it.filePath());
}


Related Topics



Leave a reply



Submit