Determine if command is recognized in a batch file

45

5

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?

user46097

Posted 2010-08-13T12:32:07.870

Reputation: 481

How will you "recognize" your command ? – Rook – 2010-08-13T12:56:10.947

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. – Rook – 2010-08-13T12:57:02.993

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 – 2010-08-13T13:18:36.697

Answers

59

WHERE mycommand
IF %ERRORLEVEL% NEQ 0 ECHO mycommand wasn't found 

Josh Santangelo

Posted 2010-08-13T12:32:07.870

Reputation: 730

11If someone doesn't want the output inside the cmd window just add >nul 2>nul behind the mycommand. – Sebastian – 2014-08-13T13:09:22.967

Note that there are more output channels than 1 and 2, since 1 stands for output buffer and 2 for error buffer, it depends on how the application was developed, it should work for common apps that ships with Windows, but an specific CLI program could still throwing text when channels 1 and 2 are redirected. – ElektroStudios – 2015-10-13T14:26:33.993

15

The code below should always execute cleanly with no garbage output.

javac -version >nul 2>&1 && (
    echo found javac
) || (
    echo fail
)

Output:

found javac

The same code as a one-liner:

javaz -version >nul 2>&1 && ( echo found javac ) || ( echo fail )

Output:

fail

Note that the order of && and || seems to matter. Also, the command whose existence you are testing for needs to return with an errorlevel <= 0 for this to work. Hopefully the command has /? or --help arguments or, as with java, a version info command.

ngreen

Posted 2010-08-13T12:32:07.870

Reputation: 251

This solution is perfect ! – robe007 – 2019-07-08T23:19:42.170

7

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.

Joey

Posted 2010-08-13T12:32:07.870

Reputation: 36 381

1This is perfect, does exactly what was asked for! – Pez Cuckow – 2013-02-18T12:26:51.070

3

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"

user46097

Posted 2010-08-13T12:32:07.870

Reputation: 481

Why don't you redirect stdout too? – Joey – 2010-08-14T13:09:37.677

3

Some refinements to version below. Test that command exists and suppress unneeded output.

WHERE scp >nul 2>nul
IF %ERRORLEVEL% EQU 0 ECHO scp found

yuliskov

Posted 2010-08-13T12:32:07.870

Reputation: 263

where <my_exe> NUL 2>&1 || echo my.exe does not exist && goto :EOF works nicely in scripts – Robert – 2015-11-10T15:01:21.140

NB: This does not work if the file name you want to test includes path information. E.g., WHERE \Windows\System32\cmd.exe => INFO: Could not find files for the given pattern(s). – Jonathan Gilbert – 2018-08-31T17:25:21.903

1

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.

Gilles 'SO- stop being evil'

Posted 2010-08-13T12:32:07.870

Reputation: 58 319

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 – 2010-08-13T13:16:26.657

2The second link Gilles gave has a nifty solution that uses FOR and no extra tools. – paradroid – 2010-08-13T13:19:14.010

1

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
)

bryan

Posted 2010-08-13T12:32:07.870

Reputation: 7 848

This is clever, I like it. – Shinrai – 2010-08-13T22:20:25.440

Yeah, but its ugly since, if the command is not found it throws a 2 line error. So, a cleaner solution could be found possibly. – djangofan – 2012-11-28T18:43:59.140

0

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

uSlackr

Posted 2010-08-13T12:32:07.870

Reputation: 8 755

5This will only look whether a file/directory with that name exists in the current directory. It provides not much of a hint whether a runnable command with that name exists because to determine that you'd have to search the PATH. – Joey – 2013-02-19T07:18:12.173