samoncode
samoncode

Reputation: 476

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

I got a directory with the following structur.

Name-
   -Year1
      -Month1
        -Day1
          -file1
        -..
      -...
   -Year2
      -Month1
      -...
   -...

Now i have 2 dates 2009-01-02 and 2010-02-03.

I want to ask if anyone knows how i can list all files which are in the folders in the given time, without building a giant if-else construct. If possible without any other frameworks than Qt.

Any help would be greatly appreciated!

Upvotes: 3

Views: 2643

Answers (3)

TWE
TWE

Reputation: 421

You can create an iterator an scan the directory tree like this:

QDir dir("YOUR_PATH_BASE"); //"Name"
if (dir.exists())
{
    foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::AllDirs ))
    {
        if (info.isFile())
        {
            //do something
        }
        if (info.isDir())
        {
            //scan again
        }
    }
}

Upvotes: 2

besworland
besworland

Reputation: 765

You can use QDir class to do this. There you can recursively iterate over all sub-folders. To do that you can use entryInfoList method. It returns a list of QFileInfo objects for all the files and directories in the directory. For every entry in this list you can check whether it is a directory or a file. If it is a directory, then you must go deeper.

Hope this helps you somehow.

Upvotes: 1

Anthony
Anthony

Reputation: 8788

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.

Upvotes: 8

Related Questions