たとえば、Windows 7mklink
では利用できますcmd.exe /C mklink
が、Windows XP では利用できません。
を実行cmd.exe /C mklink
して読み取ろうとする以外errorlevel
に、cmd.exe がコマンドをサポートしているかどうかをテストする簡単な方法はありますか?
ありがとう!
たとえば、Windows 7mklink
では利用できますcmd.exe /C mklink
が、Windows XP では利用できません。
を実行cmd.exe /C mklink
して読み取ろうとする以外errorlevel
に、cmd.exe がコマンドをサポートしているかどうかをテストする簡単な方法はありますか?
ありがとう!
ERRORLEVEL
ofcmd
は、コマンドが存在しないか失敗した場合にゼロ以外の値に設定されるため、コマンドの存在を示す適切な指標ではありません。これにより、テストが中断される可能性があります。
または、次のいずれかを実行できます。
Adriano がコメントで提案したように、次のように Windows のバージョンを確認できます。
set mklink_supported=true
ver | find "XP" >nul 2>&1 && set mklink_supported=false
またはそのように:
set mklink_supported=false
echo %vers% | find "Windows 7" >nul 2>&1 && set mklink_supported=true
その後:
if %mklink_supported%==false (
echo 'mklink' is not supported on this operating system.
)
またはこれらの線に沿った何か。ただし、必要なすべての OS バージョンを処理していることを確認する必要があります。
ERRORLEVEL
または、直接実行を試みることもできますmklink
。見つからない場合は、次のようにERRORLEVEL
設定され9009
ます。
@echo off
mklink >nul 2>&1
if errorlevel 9009 if not errorlevel 9010 (
echo 'mklink' is not supported on this operating system.
)
-ステートメントが 2 つあることに注意してくださいif
。>=9009のif errorlevel 9009
場合に機能するため、>9009の場合を除外するには 2 番目のステートメントが必要です)。ERRORLEVEL
if
ERRORLEVEL
Windows のすべてのバージョンで動作することが期待されるため、2 番目のソリューションを好みます。
@echo off
(for /f %%F in ('help') do echo '%%F ')|findstr /i /c:"%1 " 2>&1 >nul && echo Supported || echo Not supported
これは、内部コマンド(およびかなりの数の外部コマンド)のかなり完全なリストがhelp
含まれているように見えるという事実に依存しています。引数としてコマンド名が必要です(isSupported.bat command_name
)
特定のコマンドが実行されるかどうかは実際にはテストされません。実行されるはずの場合にのみテストされます...
これは単なるアイデアです。無効にしてみてください。無効にすると、喜んで削除します。
実行可能ファイルを見つけるには、for
ループで変数展開を使用できます。
setlocal EnableDelayedExpansion
set found=no
for %%f in (mklink.exe) do if exist "%%~$PATH:f" set found=yes
echo %found%
endlocal
このバッチ スクリプトは、少し前に SS64 Windows CMD Shell フォーラムに投稿しました。これは、wmz と Ansgar Wiechers の回答にあるアイデアを 1 つの便利なパッケージにまとめたものです。
指定された実行可能ファイルを PATH のどこかに見つけようとし、見つからない場合は HELP を検索します。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