[SOLVED] Get extension of a file in Qt
-
wrote on 1 Apr 2015, 14:54 last edited by ashishbansal 4 Feb 2015, 07:02
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 -
wrote on 1 Apr 2015, 15:53 last edited by
-
wrote on 1 Apr 2015, 15:57 last edited by
Another way would be to find the first position on the dot "."
and take the right part as the extension -
Another way would be to find the first position on the dot "."
and take the right part as the extensionwrote on 1 Apr 2015, 16:42 last edited by@maximus said:
Another way would be to find the first position on the dot "."
and take the right part as the extensionBut that too is not gonna work. Like in foo.bar.tar.gz, extension is tar.gz but it would give me bar.tar.gz
-
wrote on 1 Apr 2015, 16:46 last edited by
Usually what comes after the fist dot is the extension
Do you really have filename named that way? I don't.. -
Usually what comes after the fist dot is the extension
Do you really have filename named that way? I don't..wrote on 1 Apr 2015, 16:48 last edited by@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. -
wrote on 1 Apr 2015, 16:56 last edited by
Can you show an actual file name (real) and what result you would like to get for the extension?
Thanks -
Can you show an actual file name (real) and what result you would like to get for the extension?
Thankswrote on 1 Apr 2015, 17:27 last edited by@maximus said:
Can you show an actual file name (real) and what result you would like to get for the extension?
ThanksFile name : freedom-1.6.apk
Result: freedom-1.6Filename : data.tar.gz
Result: dataFilename: script.sh
Result: scriptFilename: qt-everywhere-opensource-src-5.3.0.tar.xz
Result: qt-everywhere-opensource-src-5.3.0I would like to get these result from the respective file names.
Thanks
Ashish Bansal -
wrote on 1 Apr 2015, 20:39 last edited by
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.
-
@maximus said:
Can you show an actual file name (real) and what result you would like to get for the extension?
ThanksFile name : freedom-1.6.apk
Result: freedom-1.6Filename : data.tar.gz
Result: dataFilename: script.sh
Result: scriptFilename: qt-everywhere-opensource-src-5.3.0.tar.xz
Result: qt-everywhere-opensource-src-5.3.0I 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.
wrote on 2 Apr 2015, 07:01 last edited byWell 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.
-
wrote on 5 Nov 2019, 12:57 last edited by
Simply use QFileInfo.completeSuffix. This will return the extension.
Hope this helps -
wrote on 5 Nov 2019, 13:43 last edited by
@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"
-
@maximus said:
Another way would be to find the first position on the dot "."
and take the right part as the extensionBut that too is not gonna work. Like in foo.bar.tar.gz, extension is tar.gz but it would give me bar.tar.gz
wrote on 6 Nov 2019, 20:03 last edited by@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.