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. QDirIterator: Missing a file
Forum Updated to NodeBB v4.3 + New Features

QDirIterator: Missing a file

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 802 Views 1 Watching
  • 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.
  • F Offline
    F Offline
    fem_dev
    wrote on last edited by
    #1

    I wrote this function that returns a file paths list of a desired folder path:

    std::list<QString> App::getFilesPath(const QString dirPath)
    {
        // Get the current folder path:
        QDir dir = QDir::currentPath();
    
        // Move to the library folder path:
        dir.cd(dirPath);
    
        // Output paths list:
        std::list<QString> paths;
    
        // For all files in the folder:
        QDirIterator it(dir.absolutePath(), QDir::AllEntries | QDir::NoDotAndDotDot,  QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
        while (it.hasNext()) {
            // Check the file extension:
            if(it.fileInfo().isFile() && it.fileInfo().completeSuffix() == "json") {
    
                // Add path to the output list:
                paths.push_back(it.filePath());
            }
    
            // Go to the next file:
            it.next();
        }
    
        return paths;
    }
    

    My problem is that:
    The dirPath that I'm passing is a local folder that contains 16 JSON files.
    But this function return a list with only 15 paths.

    Why?

    KroMignonK 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #6

      @fem_dev said in QDirIterator: Missing a file:

      I'm missing the last one...the 16...

      So did you actually add a debug output in the loop before and after your check?

      /edit: you're calling iter.next() too late.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        Hi,

        Maybe a silly question but are you sure that all their extension are using the same casing ?

        And that they are all really ending with .json ?

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

        1 Reply Last reply
        3
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Simply print out which files are added to see which one is missing.

          btw: std::list is for sure not what you want...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • F fem_dev

            I wrote this function that returns a file paths list of a desired folder path:

            std::list<QString> App::getFilesPath(const QString dirPath)
            {
                // Get the current folder path:
                QDir dir = QDir::currentPath();
            
                // Move to the library folder path:
                dir.cd(dirPath);
            
                // Output paths list:
                std::list<QString> paths;
            
                // For all files in the folder:
                QDirIterator it(dir.absolutePath(), QDir::AllEntries | QDir::NoDotAndDotDot,  QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
                while (it.hasNext()) {
                    // Check the file extension:
                    if(it.fileInfo().isFile() && it.fileInfo().completeSuffix() == "json") {
            
                        // Add path to the output list:
                        paths.push_back(it.filePath());
                    }
            
                    // Go to the next file:
                    it.next();
                }
            
                return paths;
            }
            

            My problem is that:
            The dirPath that I'm passing is a local folder that contains 16 JSON files.
            But this function return a list with only 15 paths.

            Why?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #4

            @fem_dev What operating system you are using?
            Do you have read access rights for all those files?
            can you show the directory content (ls or dir output)?

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            2
            • F Offline
              F Offline
              fem_dev
              wrote on last edited by
              #5

              @SGaist yes...please look this image...
              @Christian-Ehrlicher I'm missing the last one...the 16...
              @KroMignon Windows 10 x64

              folder.png
              dir.png

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #6

                @fem_dev said in QDirIterator: Missing a file:

                I'm missing the last one...the 16...

                So did you actually add a debug output in the loop before and after your check?

                /edit: you're calling iter.next() too late.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                F 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @fem_dev said in QDirIterator: Missing a file:

                  I'm missing the last one...the 16...

                  So did you actually add a debug output in the loop before and after your check?

                  /edit: you're calling iter.next() too late.

                  F Offline
                  F Offline
                  fem_dev
                  wrote on last edited by fem_dev
                  #7

                  @Christian-Ehrlicher

                  while (it.hasNext()) {
                      qDebug() << "Before: " << it.filePath();
                      if(it.fileInfo().isFile() && it.fileInfo().completeSuffix() == "json") {
                          qDebug() << "After: " << it.filePath();
                  

                  Result: Is missing the 16 file:

                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR001.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR001.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR002.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR002.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR003.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR003.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR004.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR004.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR005.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR005.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR006.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR006.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR007.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR007.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR008.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR008.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR009.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR009.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR010.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR010.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR011.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR011.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR012.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR012.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR013.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR013.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR014.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR014.json"
                  Before:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR015.json"
                  After:  "C:/Users/lamar/Desktop/rotortest/library/materials/LAMAR/LMR015.json"
                  Before:  ""
                  

                  Why?

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

                    As @Christian-Ehrlicher already wrote, you are calling next too late in your loop.

                    It's the first thing you should do.

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

                    1 Reply Last reply
                    1
                    • F Offline
                      F Offline
                      fem_dev
                      wrote on last edited by
                      #9

                      thank you all!!

                      1 Reply Last reply
                      0

                      • Login

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