Question

I was making a program for entertainment in batch language, but there is a big bug:

@echo off  
echo SimpleCmd is loading......  
for /l %%i in (1,1,100000) do rem  
cls  
echo Loading end.  

:ASKFORINPUT  
set /p input=">>"  
call :LoCase input  
set input_bool=1  
findstr /i /r /c:"^[ ]*:%input%\>" "%~f0" >nul 2>nul && goto %input%  

::FUNCTION  

:system  
:system.menu  
echo ---------MENU---------  
echo ^| 1. System Command  ^|  
echo ^| 2. Back to Input   ^|  
if "%errorlevel%"=="1" (goto system.command)  
if "%errorlevel%"=="2" (goto ASKFORINPUT)  
:system.command  
set /p systemcommand="cmd>>"  
if %systemcommand%==simplecmd.back goto ASKFORINPUT  
%systemcommand%  
goto system.command  

:random  
:random.lowerlimit  
set /p lowerlimit="Insert Lower Limit (1-32767) : "  
if %lowerlimit% GTR 32767 (goto random.lowerlimit)  
if %lowerlimit% LSS 1 (goto random.lowerlimit)  
:random.upperlimit  
set /p upperlimit="Insert Upper Limit (2-32768) : "  
if %upperlimit% GTR 32768 (goto random.uppwerlimit)  
if %upperlimit% LSS 2(goto random.upperlimt)  
:random.generate  
set /a number.random=%random%*%upperlimit%/32768+%lowerlimit%  
goto ASKFORINPUT  

::FUNCTION -END  

::LOCASE  
:LoCase  
if %input_bool%==1 goto ASKFORINPUT  
set input=%input:Q=q%  
set input=%input:W=w%  
set input=%input:E=e%  
set input=%input:R=r%  
set input=%input:T=t%  
set input=%input:Y=y%  
set input=%input:U=u%  
set input=%input:I=i%  
set input=%input:O=o%  
set input=%input:P=p%  
set input=%input:A=a%  
set input=%input:S=s%  
set input=%input:D=d%  
set input=%input:F=f%  
set input=%input:G=g%  
set input=%input:H=h%  
set input=%input:J=j%  
set input=%input:K=k%  
set input=%input:L=l%  
set input=%input:Z=z%  
set input=%input:X=x%  
set input=%input:C=c%  
set input=%input:V=v%  
set input=%input:B=b%  
set input=%input:N=n%  
set input=%input:M=m%  
set input_bool=0  
::LOCASE -END  

That's a easy program; but the bug is:

Goto was unexpected at this time

This message is shown just after I typed "system.command" and pressed enter.
I have read the lines again and again, but just cannot see the problem.
Is there any problem on this line : findstr /i /r /c:"^[ ]*:%input%\>" "%~f0" >nul 2>nul && goto %input%?

Any help will be appreciated.

Was it helpful?

Solution

At this point, input_bool would be undefined, so

if %input_bool%==1 goto ASKFORINPUT

is resolved as

if ==1 goto ASKFORINPUT

IF syntax is if string1 operator string2 action.

string1 is ==1; operator is goto. goto is not an operator that if understands, so it spits out an objection.

You need if "%possiblyemptyvariable%"=="1" goto ...

But - not a good idea to GOTO in a called routine. goto :eof to exit the routine if you must, else your flow-control will get out-of-hand.

OTHER TIPS

Change this

if %input_bool%==1 goto ASKFORINPUT  

to this

echo if %input_bool%==1 goto ASKFORINPUT  
pause

and you will see the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top