批处理文件 - If 语句和转到错误


放入

此批处理文件的这一部分的任何内容都会自动转到:yes,无论该字符串是否包含在MRSVANDERTRAMP中.txt。

:enterverb
set /p id=Enter Verb: 
findstr /m %id% MRSVANDERTRAMP.txt
if %errorlevel%==0 (
goto :yes 
) else (
goto :no
)
:no
echo Verb does not take etre in the Perfect Tense. 
pause
goto :option0
:yes
echo Verb is a MRSVANDERTRAMP verb and takes etre in the Perfect Tense.
pause

我想出了一个类似的代码来测试它,它可以工作。

@echo off
@title TEST
:main
set /p word=Write a word: 
findstr /M %word% words.txt
if "%errorlevel%" == "0" ( echo FOUND ) else ( echo NOT FOUND )
pause>nul
cls && goto main




也许 IF 语句有问题。尝试使用
if "%errorlevel%" == "0" 

欧洲工商管理学院

if %errorlevel%==0

这工作正常。 您可能希望如图所示用双引号将"%id%"括起来,以防止某些有害字符。

@echo off
echo one>MRSVANDERTRAMP.txt
echo two>>MRSVANDERTRAMP.txt

:enterverb
set /p id=Enter Verb: 
findstr /m %id% MRSVANDERTRAMP.txt
if %errorlevel%==0 (
goto :yes 
) else (
goto :no
)
:no
echo Verb does not take etre in the Perfect Tense. 
pause
goto :option0
:yes
echo Verb is a MRSVANDERTRAMP verb and takes etre in the Perfect Tense.
pause
:option0
echo done
pause

>伙计试试这个

动词

set /p id=Enter Verb: 
findstr /m %id% MRSVANDERTRAMP.txt
set a=0
if %errorlevel%== %a% (
goto :yes 
) else (
goto :no
)
:no
echo Verb does not take etre in the Perfect Tense. 
pause
goto :option0
:yes
echo Verb is a MRSVANDERTRAMP verb and takes etre in the Perfect Tense.

我在这段代码中遇到了同样的问题:

@echo off
ping www.google.com
if %errorlevel%== 0 (
goto :ok
) else (
go to :fail
)
:ok
echo ok
pause
:fail
echo fail
pause

总是去:ok但是有了一var=0我的问题就解决了,我的代码也运行了。

@echo off
ping www.google.com
set a=0
if %errorlevel%== %a% (
goto :ok
) else (
goto :fail
)
:ok
echo ok
pause

:fail
echo fail
pause

最新更新