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



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



  • 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



  • 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".



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



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

    So, how's your version part constructed?



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


Log in to reply
 

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