Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. checking files in all sub-directories
Forum Update on Monday, May 27th 2025

checking files in all sub-directories

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • QT_QT_QTQ Offline
    QT_QT_QTQ Offline
    QT_QT_QT
    wrote on last edited by QT_QT_QT
    #1

    Hey guys! Give you a scenario . You have a folder A, inside the folder you ll have another three folders, the name of these three folder inside folder A are B,C and D. Now, Folder B and D is a empty folder, which means the folders B and D dont have anything inside the folder, but for folder C ,yes, its not empty, there is an another empty file called folder E which located inside folder C. Right now, i want to check the folder E in which folder , want to check either is in B,C or D. Let me make my question simpler, how do i check which files have the folder E in folder A?
    FYI, i have already implement some code for this scenario . Here the codes:

    void CheckForFiles_Form ::recursiveAddDir(QDir d, bool recursive)
    {
    
        d.setSorting(QDir::Name);
        QDir::Filters df = QDir::Files | QDir::NoDotAndDotDot;
        if(recursive) df |= QDir::Dirs;
        QStringList qsl = d.entryList  (df,QDir::Name|QDir::DirsFirst);
        foreach (const QString &entry, qsl){
            QFileInfo fInfo(d,entry);
            if(fInfo.isDir()){
                QDir sd(fInfo.absoluteFilePath());
                recursiveAddDir(sd,true);
            }else{
                if(**fInfo.completeSuffix()== "txt"**)
                               qDebug() << fInfo.filePath();
    
    
    void CheckForFiles_Form::on_pushButton_clicked()
    {
        QDir r("C:/Users/USER/Desktop/FileA/");
        recursiveAddDir(r, true);
    }
    

    Above codes can only look for extension of a file , eg: ..mp3,..zip and *.txt . This is not what i want, what i wanted is to look for the name of the folder .not the extension. I tried modify the codes that i provided, what have i modified is instead of fInfo.completeSuffix()=="txt" , i changed to fInfo.completeBaseName()=="fileE". The modification that i made will give you no error when you compile and build, but, no result will display out when you try to execute this function . Some one here please guide me , i already done most of the implementation of this problem , right now i just need to do some modification in order to meet my requirement. thank you!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You should check with qDebug what files it really finds
      Also
      fInfo.completeBaseName() should be it.
      So QDebug all you compare.
      also try
      fInfo.completeBaseName().Contains("fileE");
      or
      http://doc.qt.io/qt-5/qstring.html#indexOf

      I am not sure == compares as you think.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Since you are looking for a folder, why not filter your entry list ask only for the Dirs ? Then it's a simple check with QStringList::contains.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        QT_QT_QTQ 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Since you are looking for a folder, why not filter your entry list ask only for the Dirs ? Then it's a simple check with QStringList::contains.

          QT_QT_QTQ Offline
          QT_QT_QTQ Offline
          QT_QT_QT
          wrote on last edited by
          #4

          @SGaist

          Hi, thanks for replying. I dont really understand the comment that you posted, perhaps you can give an example, so that i can have a clear picture. if(recursive) {df |= QDir::Dirs;} , isn't this line to ask only for dir? thank you.

          1 Reply Last reply
          0
          • QT_QT_QTQ Offline
            QT_QT_QTQ Offline
            QT_QT_QT
            wrote on last edited by
            #5
             QString root = "C:/Users/USER/Desktop/FileA/";
                QDirIterator it(root,QStringList() << "fileE",QDir::Files,QDirIterator::Subdirectories);
                        while(it.hasNext())
                {
                    qDebug() << it.next();
                    qDebug() << it.fileName();
                }
            

            tried to list all the directories and perform the checking operation unto it. But , this only work with file extension. Initially, its was "*.txt" , but i changed to "fileE" , it will give no errors when you try to compile. However, it doesn't produce any result . I m really dont have clues to proceed anymore. All i want to list all subdirectories, perform a checking operation unto the sundiretories, and lastly return an absolute path for the result . Thank you

            the_T 1 Reply Last reply
            0
            • QT_QT_QTQ QT_QT_QT
               QString root = "C:/Users/USER/Desktop/FileA/";
                  QDirIterator it(root,QStringList() << "fileE",QDir::Files,QDirIterator::Subdirectories);
                          while(it.hasNext())
                  {
                      qDebug() << it.next();
                      qDebug() << it.fileName();
                  }
              

              tried to list all the directories and perform the checking operation unto it. But , this only work with file extension. Initially, its was "*.txt" , but i changed to "fileE" , it will give no errors when you try to compile. However, it doesn't produce any result . I m really dont have clues to proceed anymore. All i want to list all subdirectories, perform a checking operation unto the sundiretories, and lastly return an absolute path for the result . Thank you

              the_T Offline
              the_T Offline
              the_
              wrote on last edited by the_
              #6

              @QT_QT_QT
              Does there any file named fileE exist in any subfolders as you use QDir::Files as filter?

              QDirIterator(const QString &path, const QStringList &nameFilters, QDir::Filters filters = QDir::NoFilter, IteratorFlags flags = NoIteratorFlags) only finds any entries, that have exact matching if you put a string without wildcards into the QStringList. So if you do not have any fileE somewhere it will be empty.

              If you want to search for folders you should use QDir::Dirs for the filter parameter

              -- No support in PM --

              1 Reply Last reply
              0
              • QT_QT_QTQ Offline
                QT_QT_QTQ Offline
                QT_QT_QT
                wrote on last edited by
                #7

                Dear All, after hours of struggling . Finally i come out with a solution ! Feel free to take a look!
                Credit to @SGaist @mrjj @the_

                //This is the fliters
                QDir::Filters df = QDir::Dirs |QDir::NoDotAndDotDot;
                    QDirIterator::IteratorFlag dff = QDirIterator::Subdirectories;
                
                
                
                    QString root = "C:/Users/USER/Desktop/FileA/";
                    QDirIterator it(root,df,dff);
                     while(it.hasNext())
                    {
                        //performing checking operation
                       //QString str = "fileE";
                         QString str2 = it.next();
                
                         if(str2.contains("fileE"))
                         {
                            qDebug()<<str2;
                         }
                
                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved