----- Original Message ----- 
From: "Arms, Mike" <[EMAIL PROTECTED]>
To: "'Dax T. Games'" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Tuesday, September 30, 2003 13:03
Subject: RE: 'which' functionality in Perl


> Dax T. Games [EMAIL PROTECTED] wrote:
> > How would I determine if a file existed in a directory in the
> > PATH environment variable on a Windows box with Perl.  If the
> > file exists I want to return the full path to the file.
> >
> > The functionality I want is similar to 'which' on Unix/Linux.
>
> Dax, I second the folks recommending the Perl Power Tools (ppt)
> if you want a straight implementation of Unix commands in Perl.
>
> I wrote my own enhanced which.pl command which could do more than
> the standard Unix "which":
>
> 1. If on a Win32 platform, it is PATHEXT aware and will match
> without typing the suffix. And in such a case it only matches
> files that have a suffix in the PATHEXT list. Example:
>
>   C:\>which gvim
>   C:\WINNT\gvim.bat
>   C:\Pkgs\vim\vim61\gvim.exe
>
> 2. Each directory in the PATH environment variable is searched
> and all matches are displayed (not just the first). You can
> see this in the example above. This is useful to determine if
> you have more than one executable of the same basename in
> various directories of your PATH. The first file with the given
> basename hides the others from being executed (unless a full
> path is specified).
>
> 3. Can type in a wildcard character '*' or '?' to have it match
> any executables with that filename pattern. Example:
>
>   C:\> which.pl jav*
>   C:\Java\j2sdk1.4.1_02\bin\java.exe
>   C:\Java\j2sdk1.4.1_02\bin\javac.exe
>   C:\Java\j2sdk1.4.1_02\bin\javadoc.exe
>   C:\Java\j2sdk1.4.1_02\bin\javah.exe
>   C:\Java\j2sdk1.4.1_02\bin\javap.exe
>   C:\Java\j2sdk1.4.1_02\bin\javaw.exe
>   C:\WINNT\system32\java.exe
>   C:\WINNT\system32\javaw.exe
>
> Another example illustrating two wildcards:
>
>   C:\>which *nd*
>   C:\Perl\bin\find2perl.bat
>   C:\Pkgs\bin\pfind.pl
>   C:\Perl\bin\find2perl.bat
>   C:\WINNT\system32\append.exe
>   C:\WINNT\system32\command.com
>   C:\WINNT\system32\expand.exe
>   C:\WINNT\system32\faxsend.exe
>   C:\WINNT\system32\find.exe
>   C:\WINNT\system32\findstr.exe
>   C:\WINNT\system32\nddeapir.exe
>   C:\WINNT\system32\rundll32.exe
>   C:\WINNT\system32\sndrec32.exe
>   C:\WINNT\system32\sndvol32.exe
>
> 4. A command line option ('-l' or '--list') to provide a
> directory listing for matching items. Example:
>
>   C:\>which -l jav*
>   2003/02/20 14:17:34     24677  C:\Java\j2sdk1.4.1_02\bin\java.exe
>   2003/02/20 14:17:34     28794  C:\Java\j2sdk1.4.1_02\bin\javac.exe
>   2003/02/20 14:17:34     28800  C:\Java\j2sdk1.4.1_02\bin\javadoc.exe
>   2003/02/20 14:17:34     28794  C:\Java\j2sdk1.4.1_02\bin\javah.exe
>   2003/02/20 14:17:34     28790  C:\Java\j2sdk1.4.1_02\bin\javap.exe
>   2003/02/20 14:17:34     28775  C:\Java\j2sdk1.4.1_02\bin\javaw.exe
>   2003/02/20 14:17:38     24677  C:\WINNT\system32\java.exe
>   2003/02/20 14:17:38     28775  C:\WINNT\system32\javaw.exe
>
> 5. A command line option ('-m' or '--md5') to compute a
> MD5 digest checksum for matching items. Example:
>
>   C:\>which -m jav*
>   9f455abce73150ed13707c1827589501  C:\Java\j2sdk1.4.1_02\bin\java.exe
>   96fa7cc38ef36a16750cdfaeb7ce7c84  C:\Java\j2sdk1.4.1_02\bin\javac.exe
>   2e2752ccf39d3d8d5654153b23ef44d0  C:\Java\j2sdk1.4.1_02\bin\javadoc.exe
>   d967925f345b70bcc2c379b0e9e65c35  C:\Java\j2sdk1.4.1_02\bin\javah.exe
>   29bf016d4642956a47364b0e0b612d36  C:\Java\j2sdk1.4.1_02\bin\javap.exe
>   2ec1d702ff5252e88e12b124c53f9099  C:\Java\j2sdk1.4.1_02\bin\javaw.exe
>   9f455abce73150ed13707c1827589501  C:\WINNT\system32\java.exe
>   2ec1d702ff5252e88e12b124c53f9099  C:\WINNT\system32\javaw.exe
>
> Note: the '-m' and '-l' options can be combined:
>
>   C:\>which -l -m java
>   9f455abce73150ed13707c1827589501  2003/02/20 14:17:34     24677
> C:\Java\j2sdk1.4.1_02\bin\java.exe
>   9f455abce73150ed13707c1827589501  2003/02/20 14:17:38     24677
> C:\WINNT\system32\java.exe
>
> This is good for finding truly duplicate executables.
>
> 6. On Win32 platforms, the current directory '.' is prepended
> to the PATH list as this is implied on Win32 systems (this
> is not implied on Unix systems). Example:
>
>   C:\Temp>which which
>   .\which.pl
>   C:\Pkgs\bin\which.pl
>
> 7. I also implement multi-level debugging and tracing to
> allow more explanation of the breakout of the filename argument
> and the PATH and PATHEXT environment variables. Example:
>
>   C:\Temp>which -# which*
>   Debugging C:\Temp\which.pl -d which
>      version  = v1.4 2003/09/14
>      debug    = 1
>      filename = which
>      PATHEXT  = .COM .EXE .BAT .CMD .VBS .VBE .JS .JSE .WSF .WSH .PL
>   Matches:
>      .\which.pl
>      C:\Pkgs\bin\which.pl
>      C:\Pkgs\bin\which0.bat
>   which.pl: Finished
>
>
> This may be overkill for your needs, but I have found
> this to be an extremely useful utility on my Win32
> platform. I add to it occasionally when I find more
> things that I want it to be able to do.
>
> You can get it here:
>
>   http://marms.sourceforge.net/perl/
>
> --
> Mike Arms


Mike,

I changed a 2 lines in your which.pl that seems to have cured the problem
with the directories with spaces in their names. Through some
experimentation, glob seems to like the forward slashes better than the
backword slashes. Also placing quotation marks around the entire string to
be globbed helped with the embedded spaces in the pathnames. Please consider
incorporating these changes into your current version.

If anyone can think of a better way to quote the argument to glob, please
let me know.

if ( $win32 )
{
    $dir =~ s!\\!/!g;
    my @glob = glob(join('','"',$dir,'/',$searchname,'.*"'));
    for my $f (@glob)
    {
       for my $ext (@pathext)
       {
           next unless $f =~ /$ext$/i && -f $f;
           print $pad . file_info( $f ) . "$f\n";
           $found++;
       }
    }
 }

Dirk Bremer - Systems Programmer II - ESS/AMS  - NISC St. Peters
USA Central Time Zone
636-922-9158 ext. 8652 fax 636-447-4471

[EMAIL PROTECTED]
www.nisc.cc

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to