PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Stop The Internet Filter!

   No Clean Feed

   The Internet Filter Is An Ex-parrot!






PGTS Humble Blog

Thread: Tips/Tricks For Programming etc

Author Image Gerry Patterson. The world's most humble blogger
Edited and endorsed by PGTS, Home of the world's most humble blogger

Which For Windows


Chronogical Blog Entries:



Date: Thu, 12 Jun 2008 22:05:48 +1000

Usually when working on a Windows workstation, I use the command line.

And often, when doing so, One of the unix commands that I wish I could use at the Windows command line is the which command. Old DOS command line warriors will recall the truncated version of the unix whereis command which was ported to PC DOS last century. It became quite popular in the era of the command line and Norton utilities for PC-DOS and MS-DOS.

Versions of the old whereis program can still be obtained (as a binary) from the Internet. However if your workstation is behind a corporate firewall, you will not be able to download binary executables ... unless you have a certain amount of ingenuity (and knowledge of firewalls). In any case there is always the risk of malware when you download Windows binaries (because the source code is not available).

In fact, the Windows version of whereis is more like which, than the latest Linux version of whereis, a far more versatile and comprehensive command that will not only search for the executable but will also discover source and man pages.

Still if you are limited in what changes you can make to the workstation, due to local security policies, and you wish to abide by those policies policies, you could create a small batch script that searches for the location of files. The following is the code for the batch file:
@echo off
@for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i

You can save this file in a folder that is in your path, and call it which.bat.

One problem with this script is it won't find the full name of an executable.

For example, this will work:

	C:\>which format
	C:\WINDOWS\system32\format.com

But this will not

	C:\>which format.com

I always use Activestate Perl in Windows, if I am able to. (Actually I usually insist that Activestate Perl is allowed to be installed on my workstation).

With Perl installed, the following script works better then the previous cmd batch script:
#!/usr/bin/perl
# which for NT -- G. Patterson Feb 2001
die "usage: $0 filename\n" unless ( @ARGV == 1);
@path = split( /;/, $ENV{PATH});
@pathext = split( /;/, $ENV{PATHEXT});
unshift ( @pathext, "");
unshift ( @path, ".");
foreach $p( @path)
{
        foreach $e( @pathext)
        {
                $p .= "\\" unless ( substr( $p, -1) eq "\\" );
                $x = $p . $ARGV[0] . lc($e);
                if ( -x $x || ( -s $x && $e) )
                {
                        my $fn = `dir /b "${x}"`;
                        if ( $fn =~ / / || $p =~ / / ) {
                                $fn =~ s/\s+$//;
                                print "\"$p$fn\"\n";
                        } else {
                                print "$p$fn";
                        }
                        exit;
                }
        }
}
exit 1;

The above script can be converted to a bat file with the pl2bat utility, and copied to a convenient location in your path.


Other Blog Posts In This Thread:

Copyright     2008, Gerry Patterson. All Rights Reserved.