Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to sort and filter files based on names with numbers in them
QtWS25 Last Chance

How to sort and filter files based on names with numbers in them

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 3 Posters 5.8k 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.
  • S Offline
    S Offline
    SavageTwinky
    wrote on last edited by
    #1

    I'm having a hard time figuring this out. We have an update folder filled with zip files basically all named similarly and their unzipped dirs.

    update1_<version>.zip
    update1_<version>
    update2_<version>.zip
    update2_<version>

    Basically my goal here is to clean out all the older updates of one particular kind of update... So I want to filter on update1 or update2, and sort by the newest to oldest version.

    I saw something about making a custom QDirModel::Sort but I'm not sure if there is an easier way...

    1 Reply Last reply
    0
    • J Offline
      J Offline
      joonhwan
      wrote on last edited by
      #2

      After seeing there is no reply to this post, maybe there is no such classes/functions that meet exactly your requirement. Though, In Qt, we have a great string handling classes like QString, QStringList, and QRegExp with which you can extract version scheme out of the file/dir name list that can also easily aquired by using QDir::entryInfoList() or QDir::entryList(). After list of file/dir name list that you want to remove, you can iterate it to remove every item easily.

      Hope this helps

      joonhwan at gmail dot com

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        There's no built-in methods for that.

        How to solve the problem depends on the actual format of the filenames. If it is restricted enough, a simple alphanumeric sort of a string list can do half of the work. The format you mentioned is not sufficient, as it will sort to

        update1_version
        update10_version
        update11_version
        update2_version
        update3_version
        ...

        Problematic can be version numbers with dots in it, these are hard to compare too.

        it will work if you add leading zeros:

        update01
        update02
        update03
        ...
        update10

        If that's not feasible, you'll have to split the actual file names into the relevant chunks and to the sorting there. Regular expressions with captions can be helpful here. "qSort() ":/doc/qt-4.8/qtalgorithms.html#qSort-2 can take a custom comparison function as an argument where you implement your own version of "is less than".

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SavageTwinky
          wrote on last edited by
          #4

          I don't think will have that problem, there are just 2 different updates, with the revision number... so it would look more like this

          ProgramA_version.zip
          ProgramB_version.zip

          and I want to sort by program and delete older versions. Granted I could use the last modified date, but someone here thinks that won't work properly... its too dangerous because it could be modified by a user...

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Using last modified date will at some point make you delete the wrong packages :)

            So, how's your version part constructed?

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SavageTwinky
              wrote on last edited by
              #6

              yah thats what I figured, we only needed to save the most recent update to update devices that havn't been up dated yet instead of redownloading an update.

              Instead I just made it so when we have a new update for ProgramA I added a filter to get everthing labled ProgramA* in the update folder and delete them, get rid of all of them before I download a newer update. Problem solved!

              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