Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-10

How to determine the file extension to use

14 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I want to launch an application via code. I know the location of the file (it will always be the same directory as the application), but I don't know the file extension. Under Linux it doesn't have an extension and under Windows it will have '.exe' extension.

So how do I determine the file extension to use so I can pass it to TProcess? I would prefer not to use IFDEF <OS>. Is there a constant similar to PathDelim, ExtensionSeparator etc I can use?

As an option I was thinking of querying the current running program (ParamStr(0)) and see what extension it has, and apply that to the application I want to launch. This should work, but I don't know if this is the best method to use.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Vincent Snijders
to Graeme Geldenhuys
Graeme Geldenhuys wrote 7 lines
2009/10/23 Vincent Snijders : > http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/getexeext.html Thanks Vincent. Should such utility functions rather be in FPC itself?

Yes, it maybe even is.

If it is added with the same name, this one can be removed. Otherwise it will remain for compatibility reasons, like
http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/dirpathexists.html and http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/directoryexists.html which was added later, IIRC.

Vincent

Reply by email

Graeme Geldenhuys
to Vincent Snijders: http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/getexeext.html Vincent

2009/10/23 Vincent Snijders :

Looking at the implementation of GetExeExt(), I'm not sure how "complete" this is. Especially if applied to other platforms. For example, OS/2 also uses ".exe" extension. Also, doesn't Mac OS X use something like .dm? extension. So I guess this function needs a bit more work before it is FPC ready. :-)

--------------------------
function GetExeExt: string;
begin
  {$IFDEF WINDOWS}
  Result:='.exe';
  {$ELSE}
  Result:='';
  {$ENDIF}
end;
--------------------------
Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Vincent Snijders
to Graeme Geldenhuys
Graeme Geldenhuys wrote 7 lines
2009/10/23 Vincent Snijders : > http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/getexeext.html Looking at the implementation of GetExeExt(), I'm not sure how "complete" this is. Especially if applied to other platforms. For example, OS/2 also uses ".exe" extension. Also, doesn't Mac OS X use

The LCL doesn't support OS/2 (yet?).

something like .dm? extension. So I guess this function needs a bit more work before it is FPC ready. :-)

AFAIK, dmg is an archiver extension (disk image), not an executable extension.

I think it is ok for the LCL.

Vincent

Reply by email

waldo kitty
to Graeme Geldenhuys: 2009/10/23 Vincent Snijders : Looking at the implementation of GetExeExt(), I'm not sure…
Graeme Geldenhuys wrote 9 lines
2009/10/23 Vincent Snijders : > http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/getexeext.html Looking at the implementation of GetExeExt(), I'm not sure how "complete" this is. Especially if applied to other platforms. For example, OS/2 also uses ".exe" extension. Also, doesn't Mac OS X use something like .dm? extension. So I guess this function needs a bit more work before it is FPC ready. :-)

i had to laugh... big belly laugh...

Show quoted text 10 lines
-------------------------- function GetExeExt: string; begin {$IFDEF WINDOWS} Result:='.exe'; {$ELSE} Result:=''; {$ENDIF} end; --------------------------

doesn't the above do the "IFDEF" thing that you said you didn't want to do? ;) :lol:

one must also remember that .exe is not the only executable extension on windows, OS/2 and DOS platforms... there is also .com... i must also ask, are not shell scripts "executable" or are we looking solely at binary executables?

seems to me that a more generic GetFileExt type function would be better... one that might also check the linux file permissions to determine if the file is executable or not... i have numerous *nix executable files with and without extensions on them... checking the perms is the only way to know if they can will start straight up or if they must be given a kickstart (ie: sh foo.sh or php foo.php or even perl foo.pl) ;)

Reply by email

Vincent Snijders
to waldo kitty
waldo kitty wrote
doesn't the above do the "IFDEF" thing that you said you didn't want to do? ;) :lol:

That is correct, let the library do the IFDEF, so you don't have to do it.

one must also remember that .exe is not the only executable extension on windows, OS/2 and DOS platforms... there is also .com... i must also ask, are not shell scripts "executable" or are we looking solely at binary executables?

That depends on the purpose. FPC cannot create .com files, so for example the file name of a fpc executable is
ExtractFileNameWithoutExt('projectfilename.lpi')+GetExeExt;

Show quoted text 8 lines
seems to me that a more generic GetFileExt type function would be better... one that might also check the linux file permissions to determine if the file is executable or not... i have numerous *nix executable files with and without extensions on them... checking the perms is the only way to know if they can will start straight up or if they must be given a kickstart (ie: sh foo.sh or php foo.php or even perl foo.pl) ;)

And that is why there is
http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/fileisexecutable.html

Vincent

Reply by email

waldo kitty
to Vincent Snijders
Vincent Snijders wrote 6 lines
waldo kitty schreef: > > doesn't the above do the "IFDEF" thing that you said you didn't want > to do? ;) :lol: That is correct, let the library do the IFDEF, so you don't have to do it.

:) but i thought the idea was for IFDEF to not be used at all... maybe i misread or simply misunderstood...

Show quoted text 8 lines
> one must also remember that .exe is not the only executable extension > on windows, OS/2 and DOS platforms... there is also .com... i must > also ask, are not shell scripts "executable" or are we looking solely > at binary executables? That depends on the purpose. FPC cannot create .com files, so for example the file name of a fpc executable is ExtractFileNameWithoutExt('projectfilename.lpi')+GetExeExt;

sure, fpc cannot create .com files but i still can and do for small quick items ;) the point being that .exe is not the only executables on these platforms...

Show quoted text 10 lines
> seems to me that a more generic GetFileExt type function would be > better... one that might also check the linux file permissions to > determine if the file is executable or not... i have numerous *nix > executable files with and without extensions on them... checking the > perms is the only way to know if they can will start straight up or if > they must be given a kickstart (ie: sh foo.sh or php foo.php or even > perl foo.pl) ;) And that is why there is http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/fileisexecutable.html

hummm... and that works on all platforms? not just *nix? i wonder if it, too, looks at the file's extension on non-*nix platforms? ;)

Reply by email

Graeme Geldenhuys
to waldo kitty: :) but i thought the idea was for IFDEF to not be used at all... maybe i misread or…
waldo kitty wrote 8 lines
> > > > doesn't the above do the "IFDEF" thing that you said you didn't want to do? ;) :lol: > > That is correct, let the library do the IFDEF, so you don't have to do it. :) but i thought the idea was for IFDEF to not be used at all... maybe i misread or simply misunderstood...

The code I'm writing is used with fpGUI Toolkit. The code I quoted is from Lazarus LCL. I was hoping the LCL or FPC's RTL implemented this in a slightly more elegant way (read that as not using IFDEF's) - but I guess not. ;-)

In all fairness, the application I'm trying to call is compiled with FPC, so the file extensions used is already greatly simplified. Always ".exe" under Windows and normally no extension under Linux, *BSD etc.. As for OS/2 - the Lazarus project might not support OS/2, but FPC sure does. So I'm not 100% sure what the default extension is for compiled executable in that case, but I would imagine ".exe" is used.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

waldo kitty
to Vincent Snijders: No need to wonder, if you actually read the link above. Vincent
Vincent Snijders wrote 11 lines
waldo kitty schreef: >> >> And that is why there is >> http://lazarus-ccr.sourceforge.net/docs/lcl/fileutil/fileisexecutable.html >> > > hummm... and that works on all platforms? not just *nix? i wonder if > it, too, looks at the file's extension on non-*nix platforms? ;) > No need to wonder, if you actually read the link above.

i have read it and the source in the .inc file as well... it indicates that, on the windows platform, a call like FileIsExecutable("foo.txt") would return true if "foo.txt" exists in the current directory... but this is incorrect because a .txt file is not executable :? and what if "foo.txt" is a directory? :?

no matter... at least there is something there to try to determine this functionality... i'll dig thru my source code archives and see if i can locate something with a li'l more meat on it's bones that i can offer for contribution ;)

Reply by email

Graeme Geldenhuys
to waldo kitty

2009/10/24 waldo kitty :

on the windows platform, a call like FileIsExecutable("foo.txt") would return true if "foo.txt" exists in the current directory... but this is incorrect because a .txt file is not executable :?

This was exactly my thought, after reading the online help. Just because the file exists, doesn't make it an executable. I did not look at the implementation though, so I cannot say if the documentation is outdated, or the implementation is lacking some serious "meat".

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email