Re: 'which' functionality in Perl
- 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 > an
Re: 'which' functionality in Perl
On approximately 9/30/2003 11:46 AM, came the following characters from the keyboard of Glenn Linderman: On approximately 9/30/2003 11:03 AM, came the following characters from the keyboard of Arms, Mike: 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": ... very interesting description deleted 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/ Hi Mike, You have a very nice which.pl there, but I see two omissions, which may be resolved by the same coding... 1) You require that an executable have an extension from PATHEXT to be found... Windows doesn't. Extensionless executables are possible, and runnable. Further, extensions not on PATHEXT are runnable by specifying the extension when running it. 2) You require that an executable be specified to which without an extension... Windows permits specification of the extension... then you add each extension from PATHEXT in turn, and therefore do not find the executable. I think both of these could be resolved by implicitly adding a "null" or "empty" extension to your internal copy of PATHEXT ... I'm not sure whether Windows checks that first or last, but you should figure that out, and do the same. For example, I placed your which.pl in a directory on my path, but .PL is not in my PATHEXT. So I get the following results: which.pl which.pl => which.pl not found in PATH which.pl which => d:\...\which.exe d:\...\which.com which.pl which.exe => which.exe not found in PATH I wouldn't have expected which.pl to be listed by "which.pl which", but I would expect it to be listed by "which.pl which.pl". And I would expect "which.pl which.exe" to find my which.exe, even though I did specify the extension. Perhaps you would prefer that it not work as I suggest, but I think that my suggestions would make it work more like Windows does. And for a which.pl that seems to be trying to work well in a Windows environment... it would seem to me that it would be a good idea to work more like Windows does. Oh, and two more bizarrenesses Given a file "foo.foo.exe" on the path, which.pl foo => d:\...\foo.foo.exe This is not expected, nor would Windows find such a thing. Also, some more bizarreness: I installed the Win2K support tools from the installation media, its default path is "C:\Program Files\Support Tools"... and it contains a number of executables, including "depends.exe". which.pl depends => depends not found in PATH however, another which.exe finds and reports it. I think you have a problem with paths containing spaces. -- Glenn -- http://nevcal.com/ === Like almost everyone, I receive a lot of spam every day, much of it offering to help me get out of debt or get rich quick. It's ridiculous. -- Bill Gates ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: 'which' functionality in Perl
On approximately 9/30/2003 11:03 AM, came the following characters from the keyboard of Arms, Mike: 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": ... very interesting description deleted 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/ Hi Mike, You have a very nice which.pl there, but I see two omissions, which may be resolved by the same coding... 1) You require that an executable have an extension from PATHEXT to be found... Windows doesn't. Extensionless executables are possible, and runnable. Further, extensions not on PATHEXT are runnable by specifying the extension when running it. 2) You require that an executable be specified to which without an extension... Windows permits specification of the extension... then you add each extension from PATHEXT in turn, and therefore do not find the executable. I think both of these could be resolved by implicitly adding a "null" or "empty" extension to your internal copy of PATHEXT ... I'm not sure whether Windows checks that first or last, but you should figure that out, and do the same. For example, I placed your which.pl in a directory on my path, but .PL is not in my PATHEXT. So I get the following results: which.pl which.pl => which.pl not found in PATH which.pl which => d:\...\which.exe d:\...\which.com which.pl which.exe => which.exe not found in PATH I wouldn't have expected which.pl to be listed by "which.pl which", but I would expect it to be listed by "which.pl which.pl". And I would expect "which.pl which.exe" to find my which.exe, even though I did specify the extension. Perhaps you would prefer that it not work as I suggest, but I think that my suggestions would make it work more like Windows does. And for a which.pl that seems to be trying to work well in a Windows environment... it would seem to me that it would be a good idea to work more like Windows does. -- Glenn -- http://nevcal.com/ === Like almost everyone, I receive a lot of spam every day, much of it offering to help me get out of debt or get rich quick. It's ridiculous. -- Bill Gates ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
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 ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: 'which' functionality in Perl
>> 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. try this http://www.perl.com/language/ppt/src/which/index.html David "Sniper" Rigaudiere ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: 'which' functionality in Perl
You know, I found a funny way of emulating "which" using Perl a few years back. This doesn't solve your problem (it's already solved by the look of Helen Bennet's email), but is more along the lines of interesting related things. If you enter this at the command prompt: perl -S notepad.exe you'll get something like the following: Unrecognized character \x90 at C:\WINDOWS\system32/notepad.exe line 1. That's because -S tells perl to search the path for whatever script you're trying to run. Since notepad.exe isn't a script, it bombs. But you get the path to the file, nonetheless. So if what you're looking for is an executable (or perhaps just not a perl script), you can use this at the command prompt to emulate "which"! jpt > -Original Message- > From: Dax T. Games [mailto:[EMAIL PROTECTED] > Sent: Saturday, September 27, 2003 5:10 PM > To: [EMAIL PROTECTED] > Subject: 'which' functionality in Perl > > > 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. > > Thanks, > > Dax > > > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE:'which' functionality in Perl
You should try this: http://search.cpan.org/~sdague/ppt-0.12/ . It provides you all the *NIX utilities working in Perl and in DOS. There are lots of these *NIX tools, nowadays (Sourceforge). But what is interesting of this one is that you can get the code and learn a lot about how they are implemented . Which works great. It was programmed by the people at PerlMonks! Then you could add the rest of the tools (LESS , MAN etc. from the complete binaries at Sourceforge http://sourceforge.net/projects/gnuwin32/ ) but that is another story. What is exciting is getting all the *NIX shell functionality with windows. -- Mensaje 8c __/~\__ (((\_/))) _) (_ - :) Alberto Adrián Schiano :) - -=( [EMAIL PROTECTED] )=- -LINUX Counter # 240133 #- come_get_my_XML-JAVA-Ebook4FREE4yourDailyThoughts
RE: 'which' functionality in Perl
On Sat, 27 Sep 2003, Helen Bennett wrote: > > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] > > Behalf Of Dax T. Games > > Sent: 27 September 2003 22:10 > > To: [EMAIL PROTECTED] > > Subject: 'which' functionality in Perl > > > > 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. > > > There is probably a module somewhere that does it.. but > the following function works as well .. may need to clean > up the items returned from path as they may have "\" on > the end, butt still works. > > Helen > > sub which ( $ ) > { > my ( $file ) = @_ ; > $path = $ENV{"PATH"} ; > foreach $dir (split (/;/, $path)) > { > return "$dir\\$file" if ( -e "$dir/$file" ) ; > } > return "not found" ; > } > print which ("cmd.exe") ; There's also a module, File::Which, which supplies a script 'pwhich' which works much like the unix 'which'. A nice feature of this module is that it'll take into account the Win32 PATHEXT environment variable, if present, so you can say, for example, C:\> pwhich cmd and it'll return \path\to\cmd.exe. -- best regards, randy kobes ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: 'which' functionality in Perl
Hi There is probably a module somewhere that does it.. but the following function works as well .. may need to clean up the items returned from path as they may have "\" on the end, butt still works. Helen sub which ( $ ) { my ( $file ) = @_ ; $path = $ENV{"PATH"} ; foreach $dir (split (/;/, $path)) { return "$dir\\$file" if ( -e "$dir/$file" ) ; } return "not found" ; } print which ("cmd.exe") ; > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > Behalf Of Dax > T. Games > Sent: 27 September 2003 22:10 > To: [EMAIL PROTECTED] > Subject: 'which' functionality in Perl > > > 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. > > Thanks, > > Dax > > > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: 'which' functionality in Perl
Title: RE: 'which' functionality in Perl Which, along with many other *nix commands, is included in the Unix95 set. I can email this to you, or you can get it from http://whizzmo.com/unix95.zip (~1meg zip file). -Original Message- From: Dax T. Games [mailto:[EMAIL PROTECTED]] Sent: Saturday, September 27, 2003 3:10 PM To: [EMAIL PROTECTED] Subject: 'which' functionality in Perl 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. Thanks, Dax ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
'which' functionality in Perl
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. Thanks, Dax ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs