Need to find a file on the execution PATH

D

davido

Hey there...

Does anyone know of a (cross platform - works in Win32 /or/ *nix) way
to figure out of a executable program is on a search path? For
example, to find if foobar.exe is in the Win32 PATH?

Googling didn't turn up suitable results (but I might have not not
used the right search criteria).

TIA.

DavidO
 
P

Paul Lalli

Hey there...

Does anyone know of a (cross platform - works in Win32 /or/ *nix) way
to figure out of a executable program is on a search path? For
example, to find if foobar.exe is in the Win32 PATH?

Googling didn't turn up suitable results (but I might have not not
used the right search criteria).


Take a look at the modules File::Find and File::Spec (specifially, the
File::Spec->path() function). Both are in the standard distribution.

Paul Lalli
 
D

davido

Take a look at the modules File::Find and File::Spec (specifially, the
File::Spec->path() function). Both are in the standard distribution.

Perfect! Do you know how to squelch the messages when find *doesn't*
find something tho? I'm seeing this:

Can't stat C:\Perl\bin\/csdfommand.com: No such file or directory
at sbt_build_deploy_tools.pm line 535
Can't stat C:\Program Files\Borland\BDS\1.0\Bin/csdfommand.com: No
such file or
directory
at sbt_build_deploy_tools.pm line 535
Can't stat c:\j2sdk1.4.1_05\bin/csdfommand.com: No such file or
directory
at sbt_build_deploy_tools.pm line 535
Can't stat C:\WINDOWS\system32/csdfommand.com: No such file or
directory
 
D

davido

Perfect! Do you know how to squelch the messages when find *doesn't*
find something tho? I'm seeing this:

Nevermind.. Apparently that was due to a "use diagnostics" statement
in my perl module.

Thanks again!
 
J

John W. Krahn

Does anyone know of a (cross platform - works in Win32 /or/ *nix) way
to figure out of a executable program is on a search path? For
example, to find if foobar.exe is in the Win32 PATH?

Googling didn't turn up suitable results (but I might have not not
used the right search criteria).

use Env '@PATH';

my $program = 'foobar.exe';

-x and print "$_\n" for map "$_/$program", @PATH;



John
 
J

Joe Smith

John said:
use Env '@PATH';

my $program = 'foobar.exe';

-x and print "$_\n" for map "$_/$program", @PATH;

Not very cross-platform. For just Win32 and *nix, I would use
my $program = 'foobar';
-x and print "$_\n" for map {"$_/$program", "$_/$program.exe"}, @PATH;

But to do it right means also checking for .bat and .cmd, and using
File::Spec so that it will work on MacOS Classic and OpenVMS.
-Joe
 
B

Ben Morrow

Joe Smith said:
Not very cross-platform. For just Win32 and *nix, I would use
my $program = 'foobar';
-x and print "$_\n" for map {"$_/$program", "$_/$program.exe"}, @PATH;

Use $Config{_exe} for more portability.
But to do it right means also checking for .bat and .cmd

Hmmm.... if you want to do this, you need to check for Win32 and then
use the appropriate environment var; I'd probably just stick to exe.
, and using
File::Spec so that it will work on MacOS Classic and OpenVMS.

....and anywhere else...

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top