[SOLVED] Get extension of a file in Qt



  • Hi Everyone,
    I was searching for efficient method for finding the file extension in Qt but I was unable to found any. Although I can break it on "." and can use the right most part as the extension but that creates problem in multiple dots file name like for file named as "foo.tar.gz", it would only give gz.

    So, Any hack-ish better solution?

    Regards
    Ashish Bansal



  • Have you checked QFileInfo

    I think this can do it easily.
    Good luck!



  • @maximus said:

    Have you checked QFileInfo

    I think this can do it easily.
    Good luck!

    But that too splits the filename(both suffix and completeSuffix). If filename is foo.bar.tar.gz, I guess it is not gonna work.



  • @ashishbansal

    Another way would be to find the first position on the dot "."
    and take the right part as the extension



  • @maximus said:

    @ashishbansal

    Another way would be to find the first position on the dot "."
    and take the right part as the extension

    But that too is not gonna work. Like in foo.bar.tar.gz, extension is tar.gz but it would give me bar.tar.gz



  • Usually what comes after the fist dot is the extension
    Do you really have filename named that way? I don't..



  • @maximus said:

    Usually what comes after the fist dot is the extension
    Do you really have filename named that way? I don't..

    Yeah I have freedom-1.6.apk
    or many other android applications having this type of name.



  • Can you show an actual file name (real) and what result you would like to get for the extension?
    Thanks



  • @maximus said:

    Can you show an actual file name (real) and what result you would like to get for the extension?
    Thanks

    File name : freedom-1.6.apk
    Result: freedom-1.6

    Filename : data.tar.gz
    Result: data

    Filename: script.sh
    Result: script

    Filename: qt-everywhere-opensource-src-5.3.0.tar.xz
    Result: qt-everywhere-opensource-src-5.3.0

    I would like to get these result from the respective file names.

    Thanks
    Ashish Bansal



  • If you had a list of known file extensions that you could compare to you could eliminate the guesses. It would be really good if you could get this list from the OS registered system file types. The list should be sorted (longest first, shortest last).

    For example, your list might contain this:

    {
    .tar.gz
    .tar.xz
    .apk
    .txt
    .bat
    .sh
    }
    

    and your program might do this:

    for counter = 0 to size_of_list
      if file_name.ends_with(extension_list[counter]) then
        // found one
      end if
    end for
    // if extension not found then resort to 'hackery'.
    

    That might be slow process if there are a lot of file extensions to check.

    Note: A file may not have an extension so looking for one might cause problems. Also, some files have a file name that begins with a dot (hidden files in GNU/Linux or Unix) and may or may not have an additional extension at the end.



  • @Rondog

    Seem like QFileInfo could do the work.

    Have you tried
    QString QFileInfo::baseName () const
    It should returns file name with longest extension removed (file.tar.gz -> file)



  • @Rondog @maximus

    Well I solved it by making use of QMimeDatabase. Thanks!

    QMimeDatabase db;
    QString name("file3.3.tar.gz");
    int length = name.length();
    QList <QPair <int, QMimeType> > list;
    for (int i = length; i > -1; i--) {
        QList<QMimeType> mimes = db.mimeTypesForFileName(name.section("", i, length));
        QMimeType mime = mimes.isEmpty() ? QMimeType() : mimes.last();
        if (mime.isValid() && (list.isEmpty() || list.last().second != mime)) {
            list.insert(list.count(), qMakePair(i, mime));
        }
    }
    if (!list.isEmpty()) {
        qDebug() << list.last();
    }
    

    As now I have i stored in list, I can take substring as answer.


Log in to reply
 

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