I'm writing a bat script in which I invoke a program (such as javac). For simplicity, I want to check if the command exists before I run it. i.e. If the command exists in PATH.

For example,

if (my_command.exe is a recognized command) then (
  my_command.exe my_args
) else (
  REM Output was probably "'my_command.exe' is not recognized as an internal or external command, operable program or batch file."
  REM Do not run my_command.exe
)

What's the best way to do this in Windows?

link|improve this question
How will you "recognize" your command ? – ldigas Aug 13 '10 at 12:56
In MS-DOS (true DOS) this was rather simple; you just checked the existence of an exe file in c:\dos; but even then the question remains. – ldigas Aug 13 '10 at 12:57
Sorry for the confusion. I meant essentially a command prompt in Windows. If I type "lkajsflksajdfj" I want to detect it isn't a command. If I type "notepad.exe", it's OK. – user46097 Aug 13 '10 at 13:18
feedback

5 Answers

If requiring the installation of extra tools is ok, there's a where command in the resource kits; see Windows equivalent of whereis?.

Otherwise, for versions of Windows that are not too ancient, it's doable in pure cmd, as mentioned in Dos executable lookup except PATH.

link|improve this answer
Thanks for the reponse! Unfortunately, one of the requirements is that it has to run on a vanilla box (XP machines included) - so whereis isn't an option. – user46097 Aug 13 '10 at 13:16
1  
The second link Gilles gave has a nifty solution that uses FOR and no extra tools. – paradroid Aug 13 '10 at 13:19
feedback

I know this not quite what you're looking for, but with a slight change in logic it should accomplish what you need.

Every command that is run has a return code (aka errorlevel), if the return code is 0 (zero), the command has run successfully, if the return code is greater than 0, something has gone wrong.

See here for more details.

Something like -

my_command
if (%ERRORLEVEL% > 0) then (
  REM Output was probably "'my_command.exe' is not recognized as an internal or external command, operable program or batch file.  OR SOMETHING WENT WRONG WITH IT."
  REM Do not run my_command.exe
)
link|improve this answer
This is clever, I like it. – Shinrai Aug 13 '10 at 22:20
feedback

The easiest way is to simply run the command, but that has other problems, of course, since maybe you don't want to have a random process started.

for %%x in (my_command.exe) do if not [%%~$PATH:x]==[] set MyCommandFound=1

is an alternative which searchs for the program in the paths listed by the %PATH% environment variable. It's essentially a pure batch version of which(1). It can be made better but essentially this is it.

link|improve this answer
feedback

While all those way might work, why not the built in way?

If exists my_command do echo "my_command exists"

Run "if /?" on the command line for details

link|improve this answer
feedback

For my situation. The absolute simplest way is using the || or && operator.

my_command.exe -version 2>NUL && echo "my_command exists"

or

my_command.exe -version 2>NUL || echo "my_command doesn't exist"
link|improve this answer
@user46097: dev/null doesn't exist on Windows; it's NUL. – Hello71 Aug 13 '10 at 14:08
Yes :). Edited. – user46097 Aug 13 '10 at 14:16
Why don't you redirect stdout too? – Joey Aug 14 '10 at 13:09
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.