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.
source share