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. How to do not list specified files
Forum Updated to NodeBB v4.3 + New Features

How to do not list specified files

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 1.1k 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.
  • eefesafakE Offline
    eefesafakE Offline
    eefesafak
    wrote on last edited by
    #1

    I can already list specified files. In addition, my goal is to do not list files starting with a specific name.
    The program and my code:

    QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
    {
        QDir qdir(directory);
    
        QString include = ui->lineEdit->text();
        QString except = ui->lineEdit2->text();
        QStringList includeList = include.split(QLatin1Char(';'));
        QStringList exceptList = except.split(QLatin1Char(';'));
    
        QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
        
        for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
        {
            fileList << getFileListFromDir(subDirectory.absoluteFilePath());
        }
        ui->lineEdit->setText(ui->lineEdit->displayText());
            return fileList;
    }
    

    Screenshot from 2022-05-12 10-44-16.png
    On the program image, I can list files ending with specific name (*.cpp and *.o) but for example, I do not want to list moc_countline.o
    Thanks for your help.

    J.HilkJ 1 Reply Last reply
    0
    • eefesafakE eefesafak

      I can already list specified files. In addition, my goal is to do not list files starting with a specific name.
      The program and my code:

      QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
      {
          QDir qdir(directory);
      
          QString include = ui->lineEdit->text();
          QString except = ui->lineEdit2->text();
          QStringList includeList = include.split(QLatin1Char(';'));
          QStringList exceptList = except.split(QLatin1Char(';'));
      
          QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
          
          for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
          {
              fileList << getFileListFromDir(subDirectory.absoluteFilePath());
          }
          ui->lineEdit->setText(ui->lineEdit->displayText());
              return fileList;
      }
      

      Screenshot from 2022-05-12 10-44-16.png
      On the program image, I can list files ending with specific name (*.cpp and *.o) but for example, I do not want to list moc_countline.o
      Thanks for your help.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @eefesafak

      for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
          {
              bool doInclude{true};
              for(const QString &entry : exceptList) {
                     if(subDirectory.baseName().startsWith(entry)) { //alternative to startsWith() -> contains() - decide for yourself what to use
                           doInclude = false;
                           break;
                      }
              }
              if(doInclude)
                    fileList << getFileListFromDir(subDirectory.absoluteFilePath());
          }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      eefesafakE 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @eefesafak

        for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
            {
                bool doInclude{true};
                for(const QString &entry : exceptList) {
                       if(subDirectory.baseName().startsWith(entry)) { //alternative to startsWith() -> contains() - decide for yourself what to use
                             doInclude = false;
                             break;
                        }
                }
                if(doInclude)
                      fileList << getFileListFromDir(subDirectory.absoluteFilePath());
            }
        
        eefesafakE Offline
        eefesafakE Offline
        eefesafak
        wrote on last edited by
        #3

        @J-Hilk I suppose this codes work only for subdirectories. Becaues, I did not get the output I want.

        J.HilkJ 1 Reply Last reply
        0
        • eefesafakE eefesafak

          @J-Hilk I suppose this codes work only for subdirectories. Becaues, I did not get the output I want.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @eefesafak my bad, this only works with filters of moc instead of moc*
          startWith has no overload for regEx, so you'll have to use contains

          if(subDirectory.baseName().contains(QRegularExpression("^" + entry) ) );


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          JonBJ 1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @eefesafak my bad, this only works with filters of moc instead of moc*
            startWith has no overload for regEx, so you'll have to use contains

            if(subDirectory.baseName().contains(QRegularExpression("^" + entry) ) );

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @J-Hilk said in How to do not list specified files:

            QRegularExpression("^" + entry)

            Won't work correctly. moc* etc. are wildcard patterns, not regular expressions, which you are assuming! (E.g. test: moc* in Exclude list will exclude moc_anything [correctly but coincidentally] but will also exclude mo_anything, which it should not.)

            Shall I leave it to you/ @eefesafak to correct? :)

            eefesafakE J.HilkJ 2 Replies Last reply
            0
            • JonBJ JonB

              @J-Hilk said in How to do not list specified files:

              QRegularExpression("^" + entry)

              Won't work correctly. moc* etc. are wildcard patterns, not regular expressions, which you are assuming! (E.g. test: moc* in Exclude list will exclude moc_anything [correctly but coincidentally] but will also exclude mo_anything, which it should not.)

              Shall I leave it to you/ @eefesafak to correct? :)

              eefesafakE Offline
              eefesafakE Offline
              eefesafak
              wrote on last edited by
              #6

              @JonB It would be great.

              JonBJ 1 Reply Last reply
              0
              • JonBJ JonB

                @J-Hilk said in How to do not list specified files:

                QRegularExpression("^" + entry)

                Won't work correctly. moc* etc. are wildcard patterns, not regular expressions, which you are assuming! (E.g. test: moc* in Exclude list will exclude moc_anything [correctly but coincidentally] but will also exclude mo_anything, which it should not.)

                Shall I leave it to you/ @eefesafak to correct? :)

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @JonB go for it, wildcards, and regex in general is magic for me

                I tinker with it until it does what I want it to, but its usually full of edge cases I did not consider :P


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • eefesafakE eefesafak

                  @JonB It would be great.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @eefesafak said in How to do not list specified files:

                  @JonB It would be great.

                  Looks like correct (Qt 5.12+) would be:

                  if (subDirectory.baseName().contains(QRegularExpression::wildcardToRegularExpression(entry) ) );
                  

                  P.S.
                  OP is Linux, so filenames are case-sensitive and hence this is correct. If you took this to Windows you would want to add QRegularExpression::CaseInsensitiveOption to the QRegularExpression::PatternOptions.

                  J.HilkJ 1 Reply Last reply
                  3
                  • JonBJ JonB

                    @eefesafak said in How to do not list specified files:

                    @JonB It would be great.

                    Looks like correct (Qt 5.12+) would be:

                    if (subDirectory.baseName().contains(QRegularExpression::wildcardToRegularExpression(entry) ) );
                    

                    P.S.
                    OP is Linux, so filenames are case-sensitive and hence this is correct. If you took this to Windows you would want to add QRegularExpression::CaseInsensitiveOption to the QRegularExpression::PatternOptions.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @JonB stats a convenient static function :D


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    0
                    • eefesafakE Offline
                      eefesafakE Offline
                      eefesafak
                      wrote on last edited by eefesafak
                      #10

                      This is my code right now and it is still not working.

                      QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
                      {
                          QDir qdir(directory);
                      
                          QString include = ui->lineEdit->text();
                          QString except = ui->lineEdit2->text();
                          QStringList includeList = include.split(QLatin1Char(';'));
                          QStringList exceptList = except.split(QLatin1Char(';'));
                      
                          QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
                      
                      
                      
                          for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
                              {
                                  bool doInclude{true};
                                  for(const QString &entry : exceptList) {
                                         if(subDirectory.baseName().contains(QRegularExpression::wildcardToRegularExpression(entry))) {
                                               doInclude = false;
                                               break;
                                          }
                                  }
                                  if(doInclude)
                                        fileList << getFileListFromDir(subDirectory.absoluteFilePath());
                              }
                      
                          ui->lineEdit->setText(ui->lineEdit->displayText());
                          return fileList;
                      }
                      

                      Screenshot from 2022-05-12 14-49-04.png

                      J.HilkJ JonBJ 2 Replies Last reply
                      0
                      • eefesafakE eefesafak

                        This is my code right now and it is still not working.

                        QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
                        {
                            QDir qdir(directory);
                        
                            QString include = ui->lineEdit->text();
                            QString except = ui->lineEdit2->text();
                            QStringList includeList = include.split(QLatin1Char(';'));
                            QStringList exceptList = except.split(QLatin1Char(';'));
                        
                            QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
                        
                        
                        
                            for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
                                {
                                    bool doInclude{true};
                                    for(const QString &entry : exceptList) {
                                           if(subDirectory.baseName().contains(QRegularExpression::wildcardToRegularExpression(entry))) {
                                                 doInclude = false;
                                                 break;
                                            }
                                    }
                                    if(doInclude)
                                          fileList << getFileListFromDir(subDirectory.absoluteFilePath());
                                }
                        
                            ui->lineEdit->setText(ui->lineEdit->displayText());
                            return fileList;
                        }
                        

                        Screenshot from 2022-05-12 14-49-04.png

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @eefesafak said in How to do not list specified files:

                        QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);

                        sorry, totally my bad. I didn't understand your code correctly.

                        You solely rely on the entryInfoList that you give a filter to to get all your files, so you would have to also give your exceptList to that.

                        However, I don't know how one "negates" a wildcard match 🤷‍♂️

                        I would personally iterate myself over the files.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1
                        • eefesafakE eefesafak

                          This is my code right now and it is still not working.

                          QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
                          {
                              QDir qdir(directory);
                          
                              QString include = ui->lineEdit->text();
                              QString except = ui->lineEdit2->text();
                              QStringList includeList = include.split(QLatin1Char(';'));
                              QStringList exceptList = except.split(QLatin1Char(';'));
                          
                              QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
                          
                          
                          
                              for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
                                  {
                                      bool doInclude{true};
                                      for(const QString &entry : exceptList) {
                                             if(subDirectory.baseName().contains(QRegularExpression::wildcardToRegularExpression(entry))) {
                                                   doInclude = false;
                                                   break;
                                              }
                                      }
                                      if(doInclude)
                                            fileList << getFileListFromDir(subDirectory.absoluteFilePath());
                                  }
                          
                              ui->lineEdit->setText(ui->lineEdit->displayText());
                              return fileList;
                          }
                          

                          Screenshot from 2022-05-12 14-49-04.png

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @eefesafak said in How to do not list specified files:

                          This is my code right now and it is still not working.

                          If you only apply the exceptList to your qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot), which only returns directories, how would you expect it to affect the files returned from the separate QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size); ??

                          Separately, since you do not apply the includeList to the directories why do you choose to apply the exceptList to them?

                          Do you follow what your own code is doing?

                          eefesafakE 1 Reply Last reply
                          2
                          • JonBJ JonB

                            @eefesafak said in How to do not list specified files:

                            This is my code right now and it is still not working.

                            If you only apply the exceptList to your qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot), which only returns directories, how would you expect it to affect the files returned from the separate QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size); ??

                            Separately, since you do not apply the includeList to the directories why do you choose to apply the exceptList to them?

                            Do you follow what your own code is doing?

                            eefesafakE Offline
                            eefesafakE Offline
                            eefesafak
                            wrote on last edited by
                            #13

                            @JonB said in How to do not list specified files:

                            Separately, since you do not apply the includeList to the directories why do you choose to apply the exceptList to them?

                            Thanks for reminding. My program is working correctly. Here is my code:

                            QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
                            {
                                QDir qdir(directory);
                            
                                QString include = ui->lineEdit->text();
                                QString except = ui->lineEdit2->text();
                                QStringList includeList = include.split(QLatin1Char(';'));
                                QStringList exceptList = except.split(QLatin1Char(';'));
                            
                                QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
                            
                                for(const QFileInfo& file : qdir.entryInfoList(QStringList() << exceptList))
                                {
                                    for(const QFileInfo& filee: fileList)
                                    {
                                        if(file.fileName() == filee.fileName())
                                        {
                                            fileList.removeOne(filee);
                                        }
                                    }
                                }
                                
                                for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
                                {
                                    fileList << getFileListFromDir(subDirectory.absoluteFilePath());
                                }
                                
                                ui->lineEdit->setText(ui->lineEdit->displayText());
                                return fileList;
                            }
                            
                            JonBJ 1 Reply Last reply
                            2
                            • eefesafakE eefesafak

                              @JonB said in How to do not list specified files:

                              Separately, since you do not apply the includeList to the directories why do you choose to apply the exceptList to them?

                              Thanks for reminding. My program is working correctly. Here is my code:

                              QFileInfoList MainWindow::getFileListFromDir(const QString &directory)
                              {
                                  QDir qdir(directory);
                              
                                  QString include = ui->lineEdit->text();
                                  QString except = ui->lineEdit2->text();
                                  QStringList includeList = include.split(QLatin1Char(';'));
                                  QStringList exceptList = except.split(QLatin1Char(';'));
                              
                                  QFileInfoList fileList = qdir.entryInfoList(QStringList() << includeList, QDir::Files, QDir::Size);
                              
                                  for(const QFileInfo& file : qdir.entryInfoList(QStringList() << exceptList))
                                  {
                                      for(const QFileInfo& filee: fileList)
                                      {
                                          if(file.fileName() == filee.fileName())
                                          {
                                              fileList.removeOne(filee);
                                          }
                                      }
                                  }
                                  
                                  for(const QFileInfo &subDirectory : qdir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot))
                                  {
                                      fileList << getFileListFromDir(subDirectory.absoluteFilePath());
                                  }
                                  
                                  ui->lineEdit->setText(ui->lineEdit->displayText());
                                  return fileList;
                              }
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @eefesafak Yep, this looks right :)

                              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