Go through the directory recursively in Qt, skip the "." Folders. and ".."

I have a little problem using Qt functions to recursively navigate through a directory. What am I trying to do:

Open the specified directory. Go through the directory and each time it encounters another directory, open this directory, go through files, etc.

Now how do I do this:

QString dir = QFileDialog::getExistingDirectory(this, "Select directory"); if(!dir.isNull()) { ReadDir(dir); } void Mainwindow::ReadDir(QString path) { QDir dir(path); //Opens the path QFileInfoList files = dir.entryInfoList(); //Gets the file information foreach(const QFileInfo &fi, files) { //Loops through the found files. QString Path = fi.absoluteFilePath(); //Gets the absolute file path if(fi.isDir()) ReadDir(Path); //Recursively goes through all the directories. else { //Do stuff with the found file. } } } 

Now, the actual problem I ran into: naturally, entryInfoList would also return '.' and "..". With this setup, this is a serious problem.

Going to "." , it will go through the entire directory twice or even endlessly (because "." is always the first element), with ".." it will repeat the process for all folders under the parent directory.

I would like to do it beautifully and smoothly, is there any way around this, I don’t know? Or is this the only way to get an explicit file name (without a path) and check it against "." . and '..'?

+6
source share
1 answer

You should try using the QDir::NoDotAndDotDot in entryInfoList , as described in the documentation .

EDIT

  • Remember to add QDir::Files or QDir::Dirs or QDir::AllFiles to receive files and / or directories as described in this post .

  • You can also check out this previous question .

+12
source

Source: https://habr.com/ru/post/923961/


All Articles