Batch file - If statement and goto error

Clash Royale CLAN TAG#URR8PPP
Batch file - If statement and goto error
Anything put into this part of this batch file automatically goes to :yes, regardless of whether the string is contained in MRSVANDERTRAMP.txt 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
Is there
:option0 tag ? Because if not the code will always go trhough the yes part.– npocmaka
Sep 10 '13 at 19:47
:option0
Yes, that is lower down (and not shown in this piece of the code).
– Max Hallam
Sep 10 '13 at 20:07
why use
goto when you can put the yes and no block in the if/else blocks already?– phuclv
Mar 11 at 2:15
goto
4 Answers
4
I came up with a 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
if "%errorlevel%" == "0"
insead of
if %errorlevel%==0
This does seem to work, and fixes another problem as well. Thanks!
– Max Hallam
Sep 10 '13 at 20:44
You're welcome! :)
– Immortal Fire
Sep 11 '13 at 11:30
This works fine. You might like to surround "%id%" in double quotes as shown, to protect against some poison characters though.
"%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
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.
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 a set off var=0 my problem is solved and my code runs.
: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
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
stackoverflow.com/questions/8530976/…
– Adam Burry
Sep 10 '13 at 19:04