Batch File 'if " "goto " " Not working

2k views Asked by At

I was creating a batch game, and I had a previous game I made so I could remember stuff from the code.

On the game I was working on the If and Goto command where not working. So I testing my previous game, and it worked fine with things like that. So I created This:

@echo off   
title test
:1
cls
echo testing Testing 123
echo Type Go
set /p 123=
if %123% == Go goto 2
if %123% == go goto 2
:3
echo Gone wrong :(
pause
exit
:2
cls
echo Worked
pause
exit

And When I type Go It says Gone Wrong :(

Can anyone help me with this?

2

There are 2 answers

0
Aacini On

You never should use a variable with a name that start in digit:

if %123% == Go goto 2

In previous line %1 is replaced by the first parameter of the Batch file, so the real comparison is:

if 23 == Go goto 2
0
npocmaka On
@echo off   
title test
:1
cls
echo testing Testing 123
echo Type Go
set /p _123=
if %_123% == Go goto 2
if %_123% == go goto 2
:3
echo Gone wrong :(
pause
exit
:2
cls
echo Worked
pause
exit

%1 is taken for the first argument to the script.You can also use delayed expansion:

@echo off  
setlocal enableDelayedExpansion 
title test
:1
cls
echo testing Testing 123
echo Type Go
set /p 123=

if !123! == Go goto :2
if !123! == go goto :2
:3
echo Gone wrong :(
pause
exit
:2
cls
echo Worked
pause
exit /b