Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QDirIterator: Missing a file



  • 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?


  • Qt Champions 2019

    @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.


  • Lifetime Qt Champion

    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 ?


  • Qt Champions 2019

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

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



  • @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)?



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

    folder.png
    dir.png


  • Qt Champions 2019

    @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.



  • @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?


  • Lifetime Qt Champion

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

    It's the first thing you should do.



  • thank you all!!


Log in to reply