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. Random folder select
Qt 6.11 is out! See what's new in the release blog

Random folder select

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 74.2k 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.
  • R Offline
    R Offline
    Rutschuru
    wrote on last edited by
    #1

    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. :(

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

      Hi,

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

      Hope it helps

      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
      0
      • R Offline
        R Offline
        Rutschuru
        wrote on last edited by
        #3

        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. :)

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

          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

          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
          0
          • R Offline
            R Offline
            Rutschuru
            wrote on last edited by
            #5

            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();
            

            @

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

              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

              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
              0
              • R Offline
                R Offline
                Rutschuru
                wrote on last edited by
                #7

                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?

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

                  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.

                  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
                  0
                  • R Offline
                    R Offline
                    Rutschuru
                    wrote on last edited by
                    #9

                    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. :/
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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

                      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
                      0

                      • Login

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