Win2K and NT Which Command

A command-shell script and VBScript file that emulate UNIX’s Which command.

Readers

April 6, 2003

2 Min Read
ITPro Today logo


[Editor's Note: Share your scripting discoveries, comments, problems, solutions, and experiences with products. Email your contributions (500 words or less) to [email protected]. We edit submissions for style, grammar, and length. If we print your submission, you'll get $100.]

The UNIX Which command lets you find in the current user's path a specified executable. By default, Which lists only the first instance of the specified file in the path. If you use the -a option, Which lists all locations of the file in any directory in the path. In UNIX, the current directory isn't in the path by default. In Windows 2000 and Windows NT, the current directory is in the path but only implicitly.

I've found the Which command so useful in UNIX that I decided to duplicate its behavior in Win2K and NT. I wrote a command-shell script, which Web Listing 1 (http://www.winscriptingsolutions.com, InstantDoc ID 38399) shows, and a VBScript file, which Web Listing 2 shows. The command-shell script uses only batch tools and relies on the native Win2K and NT tool findstr.exe. The VBScript file requires Windows Script Host (WSH). You can use Win2K's default version of WSH; for NT, you must manually install a version of WSH. Both of my scripts validate command-line syntax and search for any file that the OS deems executable. Both scripts also explicitly include the current directory.

The command-shell script contains some unusual logic to delimit certain strings correctly. This script also breaks some For loops, jumping in and out of them based on certain conditions. I repeated one line, which tests for the existence of the command issued as the argument, because the elements I needed to execute based on the command's success overran the command-line character limit.

The VBScript file is simpler than the command-shell script. I use the VBScript file solely from the command line, so I've written a small batch wrapper for the file, which Web Listing 3 shows. You must write the full path to which.vbs into the wrapper batch file.

—Glen Campbell
[email protected]

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like