- Bohack - https://www.bohack.com -

Windows Version of the Linux / UNIX ‘which’ Command

During the day I always have a command window open on my desktop. It helps get simple tasks done; like reseting a password for a user or opening an ssh session with a remote box. I have a multitude of Windows command or cmd scripts that help me through out the day. Often I may want to change or tweak a script and then the hunt is on… Which directory was it created in? I know that it is in the path; because I can just execute it without typing it’s full path, but where is it?


As best practice; I recommend to keep all scripts in a folder at the base of the %systemdrive% or C: drive and include it in your system %path% variable. This makes it easy to take your tools with you, when you redo your laptop or desktop. I personally use a folder called ‘sys’, but you can select your own. Just be sure to include the folder in your %path% variable.

Even if you put all your files in a particular folder sometimes your just looking for that particular file in the path. So I have written a Windows version of the UNIX ‘which’ command. Just copy the script below and save it as ‘which.cmd’ in your ‘sys’ folder.

Copy and Paste the script below into notepad and save it as “which.cmd”

@echo off
rem /
rem / which.cmd - 110108 - bohack.com 2008i     /
rem / Windows version of the UNIX which command /
rem /
setlocal
if [%1]==[] goto :errhand
set whichpath=%~dp$path:1
if [%whichpath%]==[] goto :end
echo %whichpath%%1
goto end

:errhand
echo.
echo Usage: %0 (filename to search in path)

:end
endlocal