Determine if command is recognized in a batch file
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"
Related videos on Youtube
user46097
Updated on September 17, 2022Comments
-
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 almost 14 yearsHow will you "recognize" your command ?
-
Rook almost 14 yearsIn 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 almost 14 yearsSorry 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 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 almost 14 yearsThanks 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 almost 14 yearsThe second link Gilles gave has a nifty solution that uses FOR and no extra tools.
-
uxout almost 14 yearsThis is clever, I like it.
-
Joey almost 14 yearsWhy don't you redirect stdout too?
-
Olivier.Roger over 11 yearsYeah, 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 over 11 yearsThis is perfect, does exactly what was asked for!
-
Joey over 11 yearsThis 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 over 8 yearsNote 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 over 8 yearswhere <my_exe> NUL 2>&1 || echo my.exe does not exist && goto :EOF works nicely in scripts
-
Jonathan Gilbert over 5 yearsNB: 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 almost 5 yearsThis solution is perfect !
-
Corentor over 3 yearsTo avoid the output of the where command, one can use the /q (quiet) switch: where /q mycommand
-
Cestarian over 3 yearsUsing the other answers in conjunction with this one is what I found best (e.g. replace javac -version with WHERE mycommand)
-
Wolf over 2 yearsYou should def. have another look onto
WHERE
as I had today :)