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

时间:2013-09-10 18:57:07

标签: batch-file

放入此批处理文件的这一部分的任何内容自动转到: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

4 个答案:

答案 0 :(得分:1)

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

@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

答案 1 :(得分: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

答案 2 :(得分:0)

老兄试试这个

enterverb

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.

答案 3 :(得分:0)

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

@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
相关问题