次の方法で共有


if

バッチ プログラムで条件付き処理を実行します。

Syntax

if [not] ERRORLEVEL <number> <command> [else <expression>]
if [not] <string1>==<string2> <command> [else <expression>]
if [not] exist <filename> <command> [else <expression>]

コマンド拡張機能が有効になっている場合は、次の構文を使用します。

if [/i] <string1> <compareop> <string2> <command> [else <expression>]
if cmdextversion <number> <command> [else <expression>]
if defined <variable> <command> [else <expression>]

Parameters

Parameter Description
not 条件が false の場合にのみコマンドを実行することを指定します。
エラーレベル <number> Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than number.
<command> 上記の条件が満たされた場合に実行する必要があるコマンドを指定します。
<string1>==<string2> Specifies a true condition only if string1 and string2 are the same. これらの値には、リテラル文字列やバッチ変数 (たとえば、%1) を指定できます。 リテラル文字列を引用符で囲む必要はありません。
存在する <filename> 指定したファイル名が存在する場合に true 条件を指定します。
<compareop> 次のような 3 文字の比較演算子を指定します。
  • EQU - Equal to
  • NEQ - Not equal to
  • LSS - Less than
  • LEQ - Less than or equal to
  • GTR - Greater than
  • GEQ - Greater than or equal to
/i 文字列の比較で大文字と小文字が区別されません。 You can use /i on the string1==string2 form of if. These comparisons are generic, in that if both string1 and string2 are comprised of numeric digits only, the strings are converted to numbers and a numeric comparison is performed.
cmdextversion <number> Cmd.exe のコマンド拡張機能に関連付けられている内部バージョン番号が、指定した数以上である場合にのみ、true 条件を指定します。 最初のバージョンは 1 です。 コマンド拡張機能に大幅な機能強化が追加されると、1 ずつ増加します。 The cmdextversion conditional is never true when command extensions are disabled (by default, command extensions are enabled).
定義 <variable> Specifies a true condition if variable is defined.
<expression> Specifies a command-line command and any parameters to be passed to the command in an else clause.
/? コマンド プロンプトにヘルプを表示します。

Remarks

  • If the condition specified in an if clause is true, the command that follows the condition is carried out. If the condition is false, the command in the if clause is ignored and the command executes any command that is specified in the else clause.

  • プログラムが停止すると、終了コードが返されます。 To use exit codes as conditions, use the errorlevel parameter.

  • If you use defined, the following three variables are added to the environment: %errorlevel%, %cmdcmdline%, and %cmdextversion%.

    • %errorlevel%: Expands into a string representation of the current value of the ERRORLEVEL environment variable. この変数は、ERRORLEVEL という名前の既存の環境変数がまだ存在しないことを前提としています。 存在する場合は、代わりにその ERRORLEVEL 値を取得します。

    • %cmdcmdline%: Expands into the original command line that was passed to Cmd.exe prior to any processing by Cmd.exe. これは、CMDCMDLINE という名前の既存の環境変数がまだ存在しないことを前提としています。 存在する場合は、代わりにその CMDCMDLINE 値を取得します。

    • %cmdextversion%: Expands into the string representation of the current value of cmdextversion. これは、CMDEXTVERSION という名前の既存の環境変数がまだ存在しないことを前提としています。 存在する場合は、代わりにその CMDEXTVERSION 値を取得します。

  • You must use the else clause on the same line as the command after the if.

Examples

メッセージを表示するには ファイルProduct.datが見つからない場合にデータ ファイル見つかりません。次のように入力します。

if not exist product.dat echo Cannot find data file

ドライブ A のディスクをフォーマットし、フォーマット処理中にエラーが発生した場合にエラー メッセージを表示するには、バッチ ファイルに次の行を入力します。

:begin
@echo off
format a: /s
if not errorlevel 1 goto end
echo An error occurred during formatting.
:end
echo End of batch program.

現在のディレクトリからファイル Product.datを削除するか、Product.datが見つからない場合にメッセージを表示するには、バッチ ファイルに次の行を入力します。

IF EXIST Product.dat (
del Product.dat
) ELSE (
echo The Product.dat file is missing.
)

Note

これらの行は、次のように 1 つの行に結合できます。

IF EXIST Product.dat (del Product.dat) ELSE (echo The Product.dat file is missing.)

バッチ ファイルの実行後に ERRORLEVEL 環境変数の値をエコーするには、バッチ ファイルに次の行を入力します。

goto answer%errorlevel%
:answer1
echo The program returned error level 1
goto end
:answer0
echo The program returned error level 0
goto end
:end
echo Done!

ERRORLEVEL 環境変数の値が 1 以下の場合に ok ラベルに移動するには、次のように入力します。

if %errorlevel% LEQ 1 goto okay