2007 Aug 9, 5:41
To satisfy my hands which have already learned to type *nix commands I like to install Win32 versions of common GNU utilities
. Unfortunately, the
command is a rather literal port and requires you to enter the entire name of the command for which you're looking. That is '
' won't find itself but
' will. This makes this almost useless for me so I thought to write my own as a batch file. I had learned about a few goodies available in cmd.exe that I thought would
make this an easy task. It turned out to be more difficult than I thought.
for /F "usebackq tokens=*" %%a in ( `"echo %PATH:;=& echo %"` ) do (
for /F "usebackq tokens=*" %%b in ( `"echo %PATHEXT:;=& echo %"` ) do (
if exist "%%a"\%1%%b (
for %%c in ( "%%a"\%1%%b ) do (
The environment variables
hold the list of paths to search through to find commands, and the extensions of files that should be run as commands
respectively. The '
for /F "usebackq tokens=*" %%a in (...) do (...)
' runs the '
' portion with
sequentially taking on the value of every line in the
' portion. That's nice, but
don't have their elements on different lines and I don't know of a way to escape a newline character to appear
in a batch file. In order to get the
's elements onto different lines I used the
syntax which replaces occurrences of a with b in the
value of ENV. I replaced the '
' delimiter with the text '
' which means
evaluates to something like "
echo .COM& echo
.EXE& echo .BAT& ...
". I have to put the whole expression in double quotes in order to escape the '&' for appearing in the batch file. The
and the backwards
quotes means that the backquoted string should be replaced with the output of the execution of its content. So in that fashion I'm able to get each element of the env. variable onto new lines. The
rest is pretty straight forward.
Also, it supports wildcards: