落ち着くのだ。
@echo off find "abc" text.txt echo %errorlevel% if errorlevel==1 GOTO LABEL2 echo find GOTO END :LABEL2 echo not find :END
Windows 7 Pro x64で試してみました。
下記の様に、GOTOからechoまでをカッコでくくると正常に動作します。
@echo off
find "abc" text.txt
echo %errorlevel%
if errorlevel == 0 (
GOTO LABEL1
:LABEL1
echo find
)
if errorlevel == 1 (
GOTO LABEL2
:LABEL2
echo not find
)
IF文はその直後の一文だけに対して作用するはずですので、元のコードではGOTOだけがIF文の分岐対象になるのではないでしょうか。
@echo off find "abc" text.txt echo %errorlevel% if errorlevel==0 GOTO LABEL1 :LABEL1 echo find goto KOKONITOBU if errorlevel==1 GOTO LABEL2 :LABEL2 echo not find goto KOKONITOBU :KOKONITOBU pause
GOTOをつかってみましょう。
if 文の書き方も間違ってるんですが、そこがあってたとしても、GOTO の飛び先が良くない。
if 文での ERRORLEVEL の扱い方も、違ってます。
よくあるプログラミング言語と違って、== のような演算子が入りません。
http://technet.microsoft.com/en-us/library/cc754335.aspx
Syntax
if [not] ERRORLEVEL <Number> <Command> [else <Expression>]
if [not] <String1>==<String2> <Command> [else <Expression>]
if [not] exist <FileName> <Command> [else <Expression>]
んで、こんな感じ。
@echo off
find "abc" text.txt
echo %errorlevel%
if ERRORLEVEL 1 GOTO LABEL1
echo find
GOTO LABEL2
:LABEL1
echo not find
:LABEL2