Darren
Darren

Reputation: 11

Filter/Exclude a directory when using QDirIterator in Qt

I'm wondering if it is possible to exclude/filter a directory when using QDirIterator. I would like it to skip over it/ignore it completely.

        QString SkipThisDir = "C:\stuff";

        QDirIterator CPath(PathToCopyFrom,  QDir::AllEntries | QDir::NoSymLinks, QDirIterator::Subdirectories );


            while(CPath.hasNext())
            {
                CPath.next();
                //DoSometing
            }

Upvotes: 1

Views: 4367

Answers (2)

pnezis
pnezis

Reputation: 12331

First of all you have to add one more backslash to your SkipThisDir in order to escape it.

Second you could do a check at the beginning of the while loop and if the current folder is the one you want to skip you could continue to the next directory.

QString SkipThisDir = "C:\\stuff";

QDirIterator CPath(PathToCopyFrom,  QDir::AllEntries | QDir::NoSymLinks, 
                   QDirIterator::Subdirectories );


while(CPath.hasNext())
{
    QString currentDir = CPath.next();
    if (currentDir == SkipThisDir)
         continue; 
    //DoSometing
}

Upvotes: 0

kenrogers
kenrogers

Reputation: 1350

I don't see anything in the API for QDirIterator that does specifically what you want. However, something as simple as the following would work.

while (CPath.hasNext())
{
    if (CPath.next() == SkipThisDir)
        continue;
    //DoSomething
}

Upvotes: 2

Related Questions