Finding files in your path environment


Occasionally I’m working at the command prompt and have a utility that is in the path environment, but need to find where the file actually is. So, I wrote a little batch file that does exactly that

@echo off
echo Searching path for %1
if "%~$PATH:1"=="" goto :notfound
echo Found: %~$PATH:1
goto :end

:notfound
echo Unable to find %1
goto :end

:end

The batch file takes a single argument which is the filename to find. The magic happens in the "%~$PATH:1" part – this basically searches the directories in the PATH environment variable for first argument passed to the batch file. All of the rest of the batch file is geared towards producing nice output!

NOTE – use this at your own risk. If it deletes all of your files or replaces the text of your important documents with Lorem Ipsum then you didn’t get it from me!

NOTE 2 – if you want to find out about the other expansion options for environment variables then type “HELP FOR” at the command prompt.

** UPDATE 2009/08/20 **:

It turns out that there’s a better way to do this: the WHERE command – thanks Rupert!

The WHERE command will list all locations that it found a match in the path, also allows wildcards and is generally more flexible. Time to delete that batch file...

I’m not sure when this command appeared. It is listed in the command-line reference for Windows Vista, but not for Windows XP.

Comments (0)

Skip to main content