Determine if command is recognized in a batch file

38,988

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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"
Share:
38,988

Related videos on Youtube

user46097
Author by

user46097

Updated on September 17, 2022

Comments

  • user46097
    user46097 over 1 year

    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?

    • Rook
      Rook almost 14 years
      How will you "recognize" your command ?
    • Rook
      Rook almost 14 years
      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.
    • user46097
      user46097 almost 14 years
      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.
    • jamesdlin
      jamesdlin about 4 years
      @Rook A search path existed in MS-DOS too (at least in later versions). Executables did not have to live in C:\DOS to be executable with just a basename.
  • user46097
    user46097 almost 14 years
    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.
  • Naidim
    Naidim almost 14 years
    The second link Gilles gave has a nifty solution that uses FOR and no extra tools.
  • uxout
    uxout almost 14 years
    This is clever, I like it.
  • Joey
    Joey almost 14 years
    Why don't you redirect stdout too?
  • Olivier.Roger
    Olivier.Roger over 11 years
    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.
  • Pez Cuckow
    Pez Cuckow over 11 years
    This is perfect, does exactly what was asked for!
  • Joey
    Joey over 11 years
    This 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.
  • ElektroStudios
    ElektroStudios over 8 years
    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.
  • Robert
    Robert over 8 years
    where <my_exe> NUL 2>&1 || echo my.exe does not exist && goto :EOF works nicely in scripts
  • Jonathan Gilbert
    Jonathan Gilbert over 5 years
    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).
  • robe007
    robe007 almost 5 years
    This solution is perfect !
  • Corentor
    Corentor over 3 years
    To avoid the output of the where command, one can use the /q (quiet) switch: where /q mycommand
  • Cestarian
    Cestarian over 3 years
    Using the other answers in conjunction with this one is what I found best (e.g. replace javac -version with WHERE mycommand)
  • Wolf
    Wolf over 2 years
    You should def. have another look onto WHERE as I had today :)