How to check if cmd.exe command exists?

As an example, mklink is available from cmd.exe /C mklink on Windows 7, but this is not the case on Windows XP.

Besides executing cmd.exe /C mklink and trying to read errorlevel is there an easier way to check if cmd.exe supports the command?

Thanks!

+4
source share
4 answers

ERRORLEVEL cmd not a good indicator for the existence of a command, since it is set to a non-zero value if either the command does not exist or it does not work, and this may throw your test away.

Alternatively, you can do one of the following:

Check OS Version

Like Adriano suggested in the comment, you can check the Windows version as follows:

 set mklink_supported=true ver | find "XP" >nul 2>&1 && set mklink_supported=false 

or so:

 set mklink_supported=false echo %vers% | find "Windows 7" >nul 2>&1 && set mklink_supported=true 

and then:

 if %mklink_supported%==false ( echo 'mklink' is not supported on this operating system. ) 

or something like that. However, you must make sure that you are working with all necessary OS versions.

Run the command and check ERRORLEVEL

Alternatively, you can try to run mklink directly. If it is not found, ERRORLEVEL set to 9009 :

 @echo off mklink >nul 2>&1 if errorlevel 9009 if not errorlevel 9010 ( echo 'mklink' is not supported on this operating system. ) 

Note that there are two if -statements. if errorlevel 9009 works if ERRORLEVEL > = 9009, so the second if -stance is necessary to exclude the case when ERRORLEVEL > 9009).

I prefer the second solution, as it is expected to work with all versions of Windows.

+3
source

To find the executable, you can use the variable extension in the for loop:

 setlocal EnableDelayedExpansion set found=no for %%f in (mklink.exe) do if exist "%%~$PATH:f" set found=yes echo %found% endlocal 
+1
source
 @echo off (for /f %%F in ('help') do echo '%%F ')|findstr /i /c:"%1 " 2>&1 >nul && echo Supported || echo Not supported 

It depends on the fact that help seems to contain a fairly complete list of internal commands (and quite a few external ones). It expects the command name as an argument ( isSupported.bat command_name )

It actually does not check if a given command is executed only if it should be there ...
This is just an idea, please try to nullify it, and I will gladly delete it if you do.

+1
source

I posted this batch script on the SS64 Shell CM64 Windows forum a while ago. It combines the ideas found in wmz and Ansgar Wiechers answers into one convenient package.

It tries to find the given executable file somewhere in PATH, and then searches for HELP if it is not found. It can give erroneous results with some ugly error messages if there is no standard utility covered by HELP.

 ::WHICH.BAT CommandName [ReturnVar] :: :: Determines the full path of the file that would execute if :: CommandName were executed. :: :: The result is stored in variable ReturnVar, or else it is :: echoed to stdout if ReturnVar is not specified. :: :: If no file is found, then an error message is echoed to stderr. :: :: The ERRORLEVEL is set to one of the following values :: 0 - Success: A matching file was found :: 1 - No file was found and CommandName is an internal command :: 2 - No file was found and CommandName is not an internal command :: 3 - Improper syntax - no CommandName specified :: @echo off setlocal disableDelayedExpansion set "file=%~1" if not defined file ( >&2 echo Syntax error: No CommandName specified exit /b 3 ) set "noExt=" setlocal enableDelayedExpansion if "%~x1" neq "" if "!PATHEXT:%~x1=!" neq "!PATHEXT!" set noExt=""; set "modpath=.\;!PATH!" @for %%E in (%noExt%%PATHEXT%) do @for %%F in ("!file!%%~E") do ( setlocal disableDelayedExpansion if not "%%~$modpath:F"=="" if not exist "%%~$modpath:F\" ( endlocal & endlocal & endlocal if "%~2"=="" (echo %%~$modpath:F) else set "%~2=%%~$modpath:F" exit /b 0 ) endlocal ) endlocal >nul help %~1 && ( >&2 echo "%~1" is not a valid command exit /b 2 ) >&2 echo "%~1" is an internal command 
0
source

Source: https://habr.com/ru/post/1436637/


All Articles