Recent

Author Topic: How to get file type from extension (multi-platform)?  (Read 344 times)

CM630

  • Hero Member
  • *****
  • Posts: 1772
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
How to get file type from extension (multi-platform)?
« on: July 22, 2026, 03:37:19 pm »
Are there some inbuilt routines to get file types from the file extensions?
I need multi-platform support.
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

LeP

  • Guest
Re: How to get file type from extension (multi-platform)?
« Reply #1 on: July 22, 2026, 04:31:09 pm »
Are there some inbuilt routines to get file types from the file extensions?
I need multi-platform support.

What do you mean with "file type" ? What do you expected ? A class, a value from a predefined list of types, a constant .... ?

Zvoni

  • Hero Member
  • *****
  • Posts: 3466
Re: How to get file type from extension (multi-platform)?
« Reply #2 on: July 22, 2026, 05:07:48 pm »
Are there some inbuilt routines to get file types from the file extensions?
I need multi-platform support.

Well, File-Types from File-Extension ("*.xlsx" = "Microsoft Excel Workbook"), those mappings are stored in the Registry (at least on Windows).
and "TRegistry" is part of the FCL, so i would hazard a guess it's cross-platform.

Would at least be my first attempt
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

CM630

  • Hero Member
  • *****
  • Posts: 1772
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: How to get file type from extension (multi-platform)?
« Reply #3 on: July 22, 2026, 05:45:25 pm »
What do you mean with "file type" ? What do you expected ? A class, a value from a predefined list of types, a constant .... ?
The attached image illustrates file types and what shall be shown.

...
Well, File-Types from File-Extension ("*.xlsx" = "Microsoft Excel Workbook"), those mappings are stored in the Registry (at least on Windows).
and "TRegistry" is part of the FCL, so i would hazard a guess it's cross-platform.

Would at least be my first attempt

Thanks, I am interested in a routine, which is already present in Lazarus/ FPC. If there is not such one, I am not going to use it (for now, at least).
« Last Edit: July 22, 2026, 05:48:29 pm by CM630 »
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

Nimbus

  • Full Member
  • ***
  • Posts: 105
Re: How to get file type from extension (multi-platform)?
« Reply #4 on: July 22, 2026, 05:47:46 pm »
https://www.freepascal.org/docs-html/fcl/fpmimetypes/tfpmimetypes.html

Maybe not exactly what you need, but perhaps the closest.

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses
  4.   fpMimeTypes;
  5.  
  6. var
  7.   T: TFPMimeTypes;
  8. begin
  9.   T := TFPMimeTypes.Create(Nil);
  10.   T.LoadKnownTypes;
  11.   WriteLn(T.GetMimeType('.jpeg'));
  12.   WriteLn(T.GetMimeType('.pdf'));
  13.   WriteLn(T.GetMimeType('.exe'));
  14.   T.Free;
  15. end.
  16.  

Code: Text  [Select][+][-]
  1. image/jpeg
  2. application/pdf
  3. application/x-msdos-program

CM630

  • Hero Member
  • *****
  • Posts: 1772
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: How to get file type from extension (multi-platform)?
« Reply #5 on: July 22, 2026, 05:52:02 pm »
https://www.freepascal.org/docs-html/fcl/fpmimetypes/tfpmimetypes.html

Maybe not exactly what you need, but perhaps the closest.

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses
  4.   fpMimeTypes;
  5.  
  6. ...
  7.  

Thanks, I already tried Mimetypes with no success.
You source results in empty lines in Windows 11.
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

Nimbus

  • Full Member
  • ***
  • Posts: 105
Re: How to get file type from extension (multi-platform)?
« Reply #6 on: July 22, 2026, 06:02:52 pm »
https://www.freepascal.org/docs-html/fcl/fpmimetypes/tfpmimetypes.html

Maybe not exactly what you need, but perhaps the closest.

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses
  4.   fpMimeTypes;
  5.  
  6. ...
  7.  

Thanks, I already tried Mimetypes with no success.
You source results in empty lines in Windows 11.

I suppose Windows 11 does not provide a default mime types file (which LoadKnownTypes tries to load). But you can create your own file (maybe just copy /etc/mime.types from Linux), and use it (LoadFromFile) in your application.

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How to get file type from extension (multi-platform)?
« Reply #7 on: July 22, 2026, 08:32:51 pm »
I name all my source files .exe, not .pas.... And all my gifs .doc.  O:-)
Any "programmer" that knows only one programming language is not a programmer

CM630

  • Hero Member
  • *****
  • Posts: 1772
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: How to get file type from extension (multi-platform)?
« Reply #8 on: July 23, 2026, 08:09:15 am »
I name all my source files .exe, not .pas.... And all my gifs .doc.  O:-)

As far as I tested, in this case:

1. Windows will show your .exe source file either as “exe file” or “application”.
I tried 3 PCs with Windows 11:
 - two of them with Explorer patcher show “application”, “Vivaldi html document”, “Lazarus project information”, etc.
 - the one without it shows “bmp file”, “lpi file”, “exe file”, “mp3 file”...
As one sample is not statistically reliable, a can only suppose that the behaviour differs due to some change in Window 11, which is overridden by Explorer patcher.

2. Kubuntu's file explorer behaves quite like Window 11 with Explorer patcher.

3. Linux Mint Mate just ignores the extension, if the file is empty and shows “Empty file”. I presumed that it uses fourCC, but when I put some text in the empty files, it started bahaving like Kubuntu.

4. I have no idea how the Balenciaga of the PCs behave.

I am not interested in this issue anymore, but if I happen to make some routines, I will post them here.
« Last Edit: July 23, 2026, 08:11:02 am by CM630 »
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018