4

たとえば、Windows 7mklinkでは利用できますcmd.exe /C mklinkが、Windows XP では利用できません。

を実行cmd.exe /C mklinkして読み取ろうとする以外errorlevelに、cmd.exe がコマンドをサポートしているかどうかをテストする簡単な方法はありますか?

ありがとう!

4

4 に答える 4

3

ERRORLEVELofcmdは、コマンドが存在しないか失敗した場合にゼロ以外の値に設定されるため、コマンドの存在を示す適切な指標ではありません。これにより、テストが中断される可能性があります。

または、次のいずれかを実行できます。

OSのバージョンを確認する

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 番目のステートメントが必要です)。ERRORLEVELifERRORLEVEL

Windows のすべてのバージョンで動作することが期待されるため、2 番目のソリューションを好みます。

于 2012-09-27T17:02:07.957 に答える
1
@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

特定のコマンドが実行されるかどうかは実際にはテストされません。実行されるはずの場合にのみテストされます...
これは単なるアイデアです。無効にしてみてください。無効にすると、喜んで削除します。

于 2012-09-27T18:31:31.060 に答える
1

実行可能ファイルを見つけるには、forループで変数展開を使用できます。

setlocal EnableDelayedExpansion
set found=no
for %%f in (mklink.exe) do if exist "%%~$PATH:f" set found=yes
echo %found%
endlocal
于 2012-09-27T17:59:40.207 に答える
0

このバッチ スクリプトは、少し前に 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
于 2012-10-05T21:09:51.607 に答える