Random folder select



  • hello,

    how can I make a random selection of a folder in a "unknow directory"?

    For example:
    @
    QFile forDelete("C:/folder1/folder2/" + unknowFolder + "/folder4/file.txt")
    forDelete.remove();
    @
    Next of ".../folder2/", theres only one "unknow Folder name" and it has a unknow name. I hope, you can help me out. :(


  • Lifetime Qt Champion

    Hi,

    You can use QDir::entryList to retrieve the content of folder2 and go on from there

    Hope it helps



  • Hello :)

    Thank you.

    I tried this:

    @
    QDir mDir("C:/folder1" + "/folder2/folder3")
    foreach(QFileInfo min, mDir.entryInfoList())
    {
    QMessageBox::information(this, "", min.absoluteFilePath());
    }

    QFile forDelete("C:/folder1/folder2/" + resultUnknowPath + "/folder4/file.txt")
    forDelete.remove();
    @

    So via Messageboxes i got this returns:

    • C:/folder1/folder2
    • C:/folder1/folder2/folder3
    • C:/folder1/folder2/folder3/unknow

    Thats good.. but I would like to get the "unknow path" and puth the name of this "unknow folder" to QFile forDelete() as (for example) "resultUnknowPath".

    Notice: I cant start my entryInfoList() at (..)/folder3 because there is more folders (folder 1 with folder1a, folder1b, folder1c, folder2 etc.).

    Thank you very much for your help, SGaist. :)


  • Lifetime Qt Champion

    Then you have to run your code recursively for each folder. So start at the root folder and go through your folder hierarchy one after the other



  • Hello SGaist, thank you for your help.

    I did it. It works. But with a "other little method". But without your help, I didnt found a "own solution" for this. I used a similar function of entryList.

    Please have a look of this code and tell me your feedback. :)

    @
    QString pathUserInput = "C:/folder1";
    QString path2Start = "/folder2/";
    QString path3Final = "";

    QDirIterator it(pathUserInput + path2Start, QDirIterator::Subdirectories);

    while (it.hasNext())
    {
        it.next();
        if(it.fileName()=="folder4")
        {
            finalPath = it.filePath();
            QMessageBox::information(this,"",finalPath); // test output
        }
    }
    
    QFile forDelete(finalPath + "/file.txt")
    forDelete.remove();
    

    @


  • Lifetime Qt Champion

    The iterator is fine.

    But if you have several folder4 you will only delete the file.txt in the last one.

    You can also use the static QFile::remove to make your code cleaner



  • Thank you SGaist. :)

    My next and last question is: How can I get a part of a foldername?

    For example:

    @
    QFile forDelete("C:/folder1/folder2/folder3/folderUNKNOW/folder5/file.txt")
    forDelete.remove();
    @

    After folder3, the first part of the name is the same: Its "folder...", but the next part of the name is "...unknow". Thats change every time.
    So I want to get the full folder name. How should I do this?

    I tried the QString split method.. but I really dont know how I do this.
    And I cant use the same iterator because I dont know the full name. I know only the "first part" of the name. How should I do this? Should I use the same Iterator with "little" changes?


  • Lifetime Qt Champion

    You can use QString's search features to get the length before folderUNKNOWN then the length up to the / following folderUNKNOWN based on the two values you can use mid() and right() to extract what you want.



  • Hello SGaist,

    I used the contains-feature of QString and iterator.

    My Problem was, that I had more "folder"-names in the subdirectory. But the first result was true. All others was false.

    I solve it with break; to get the first result.

    @
    while (it.hasNext())
    {
    it.next();
    QString str = it.fileName();
    if(str.contains("number", Qt::CaseSensitive)) // for example: foldername: number33
    {
    QMessageBox::information(this,"",it.filePath());
    break;
    }
    }
    @

    So this works. But I think theres a lot more "cleaner" and "effective" methods for this. What do you think? Im sorry. Im a truly beginner. :)

    Im going to try your method too soon. But the problem, thats the length is not fixed. So it can change to number34 from number33, or to number551 from number33.
    If i use the Iterator to check all entrys with the name "number", so It returns this:

    • number33 (thats what i want) - Folder
    • (...)/directsnumber - Subfolder
    • numberlist.txt - File
    • etc.
      I think, i use the wrong function. I think, I need something similar that returns all folder that contains "number" in the name instead of all folder and files. :/

  • Lifetime Qt Champion

    If you know you'll have several files to find, just traverse your folder tree and create a list of files to delete. Once you have search all folders, delete the files from the list


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.