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. [SOLVED] Get extension of a file in Qt
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Get extension of a file in Qt

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 24.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.
  • A Offline
    A Offline
    ashishbansal
    wrote on last edited by
    #3

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

    KroMignonK 1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by
      #4

      @ashishbansal

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


      Free Indoor Cycling Software - https://maximumtrainer.com

      A 1 Reply Last reply
      1
      • M maximus

        @ashishbansal

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

        A Offline
        A Offline
        ashishbansal
        wrote on last edited by
        #5

        @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

        W 1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by
          #6

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


          Free Indoor Cycling Software - https://maximumtrainer.com

          A 1 Reply Last reply
          0
          • M maximus

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

            A Offline
            A Offline
            ashishbansal
            wrote on last edited by
            #7

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #8

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


              Free Indoor Cycling Software - https://maximumtrainer.com

              A 1 Reply Last reply
              0
              • M maximus

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

                A Offline
                A Offline
                ashishbansal
                wrote on last edited by
                #9

                @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

                M 1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rondog
                  wrote on last edited by
                  #10

                  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.

                  A 1 Reply Last reply
                  1
                  • A ashishbansal

                    @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

                    M Offline
                    M Offline
                    maximus
                    wrote on last edited by
                    #11

                    @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)


                    Free Indoor Cycling Software - https://maximumtrainer.com

                    1 Reply Last reply
                    0
                    • R Rondog

                      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.

                      A Offline
                      A Offline
                      ashishbansal
                      wrote on last edited by
                      #12

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

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        CodingCobra
                        wrote on last edited by
                        #13

                        Simply use QFileInfo.completeSuffix. This will return the extension.
                        Hope this helps

                        1 Reply Last reply
                        0
                        • A ashishbansal

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

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #14

                          @ashishbansal said in [SOLVED] Get extension of a file in Qt:

                          I guess it is not gonna work

                          Why of course QFileInfo::completeSuffix() is working! Do you have made a try before telling it don't?

                          QFileInfo fi("/tmp/archive.tar.gz");
                          QString ext = fi.completeSuffix();  // ext = "tar.gz"
                          

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          0
                          • A ashishbansal

                            @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

                            W Offline
                            W Offline
                            wrosecrans
                            wrote on last edited by
                            #15

                            @ashishbansal said in [SOLVED] Get extension of a file in Qt:

                            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

                            Basically any library would have "bar.tar.gz" as the full extension for "foo.bar.tar.gz" -- there's nothing about the filename itself that makes bar and tar obviously distinct. So the only way to have tar.gz get picked out as special is try to try have a giant database of every possible file extension in the universe. And even if you had that, it would give unintuitive/inconsistent results tomorrow when somebody came out with a new one and it started seeing some but not others.

                            In practice, pretty much every general purpose library is just going to be splitting the name using generic rules.

                            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