Batch file - if statement and goto error

Everything that fits into this part of this batch file automatically goes to: yes, regardless of whether the line is in the MRSVANDERTRAMP.txt file or not.

: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
source share
4 answers

I came up with similar code to test it and it works.

 @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 


<h / ">
Maybe the problem is with the IF statement. Try using

 if "%errorlevel%" == "0" 

insead of

 if %errorlevel%==0 
0
source

It works great. You could bypass "%id%" in double quotes as shown to protect against some poisonous characters.

 @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 
0
source

Dude try this

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. 
0
source

I had the same problem in this code:

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

Always go to :ok but with the task var=0 my problem is solved and my code works.

 @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 
0
source

Source: https://habr.com/ru/post/1501540/


All Articles