I'm a new batch programmer and am creating a batch "start menu". I'm using the following text and it's saying "X" was not expected at this time. X was 1, game, text, and other things. I can't figure it out. HELP! I'm running XP SP3 and using the command prompt. Also, if anyone spots any other mistakes please inform me. e-mail me at superzemus#hotmail.com
cls
#echo off
echo Welcome to the Start Menu!
echo.
echo Press ctrl-z to exit. Press G to play Adventure. Press T to enter Text Editor.
pause
set Game= %gme%
set Text= %txt%
IF select Game goto gme
IF select Text goto txt
:txt
echo You have chosen to enter the Text editor.
pause
start edit
:gme
echo You have chosen to play Adventure.
pause
start C:\Adventure.exe
What you want here is probably either set /p:
set /p choice=Press ctrl-z to exit. Press G to play Adventure. Press T to enter Text Editor.
and then compare as follows:
if /i %choice%==G goto Game
if /i %choice%==T goto Text
goto :eof
Or use choice which doesn't require you to press Return:
choice /m "Press Q to exit. Press G to play Adventure. Press T to enter Text Editor." /c GTQ /n
if errorlevel 3 goto :eof
if errorlevel 2 goto Text
if errorlevel 1 goto Game
goto :eof
There is also another problem with your code: You should exit the batch file after starting the editor to avoid starting the game too:
:Text
start edit
goto :eof
:Game
rem That's a horrible place to put something
start C:\Adventure.exe
goto :eof
Related
I'm in the process of creating a mini-game using batch and one of the useful tool ideas was to have an interactive notepad so that users could store information throughout the game and refer back to it later. So far I have created the option to goto a notepad within an In-game pause menu but wasn't sure if it was possible to save results without outputting to new file on the desktop
:PauseMenu
cls
echo.
echo %Alias%
echo.
echo Notepad
echo Stats
echo Untitled2
echo Untitled3
echo Untitled4
echo Untitled5
echo Untitled6
set/p PauseMenu="N, S"
IF ["%PauseMenu%"]==["N"] goto Notepad
IF ["%PauseMenu%"]==["S"] goto Stats
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
IF ["%PauseMenu%"]==["N"] goto
Any help would be appreciated, thanks.
PS is it possible to go back to the previous page from a menu?
Simplicity itself.
First, some renaming may be in order. notepad is a supplied utility and pausemenu is being used both as a variable and as a label. This is not invalid, but can be a little confusing.
Further, if you are choosing between a set of keys, I'd suggest you investigate choice. choice has a number of advantages, like it only accepts one character, no enter is required and it's not necessary to analyse the entry.
So: revising your code:
:p_pausemenu
pause
:PauseMenu
cls
echo.
echo %Alias%
echo.
echo N Notepad
echo S Stats
echo 1 Untitled2
echo Z Untitled3
echo Q Untitled4
echo J Untitled5
echo X Untitled6
:: Note that the processing of ERRORLEVEL must be in reverse order
choice /c ns1zqjx
if errorlevel 7 goto labelx
if errorlevel 6 goto labelj
if errorlevel 5 goto labelq
if errorlevel 4 goto labelz
if errorlevel 3 goto label1
if errorlevel 2 goto stats
if errorlevel 1 goto unotepad
:unotepad
start "Notes for %alias%" notepad "c:\gamedirectory\%alias%.txt"
goto pausemenu
:stats
:: List your stats here
echo Stats for %alias%
goto p_pausemenu
Here, a menu with a number of unimplemented options is presented and the choice command (see choice /? from the prompt for more options) waits for a choice to be made.
errorlevel is set according to the choice made - but since if errorlevel n means if errorlevel is n OR GREATER THAN n you need to process errorlevel in reverse order.
Then each selection is processed. n will start a notepad instance and load the alias.txt file from the game directory, then present the menu again as it returns to pausemenu. s will show the stats (idk what you need for that) and then return to p_pausemenu which will pause and then proceed to show the menu when the user signals to do so.
I am trying to learn Batch.
I wrote a simple code with two labels.
This is the code:
#echo off
title intro
color 1f
::############################
::label
:one
cls
echo hello
echo. please enter your First name
echo.
:: fname is a variable (type p - string) that will content the user input. the input will be insert after <<
set /p fname= ">>"
echo.
echo. please enter your Last name
set /p lname=">>"
echo.
echo hello %fname% %lname%!
pause>nul
goto two
::############################
:two
cls
echo. welcome to page two
After the command line prints "hello ", it waits for the user response. if the user hits enter, instead of continuing to label "two", the command line closes.
Why?
Thanks.
Thats because it exits when the script comes to and end and has nothing more to do.
Add for example a pause at the very end of your script
Like this
echo. welcome to page two
pause
Then all you want on "page two" will be between your welcome to page two and pause
You don't say whether you are executing that batch from the prompt or from a shortcut"
After showing hello names entered the batch stops at the pause, and waits for an Enter. The >nul suppresses the prompt Press any key to continue . . .
batch will then continue to :two (the goto is redundant) but if you executed the routine from a "shortcut" the batch window will appear to close immediately as reaching end-of-file terminates the routine.
i am new here so i'll try to be as good as i can.
So i am trying to make a RPG based on text-based MS-DOS, and i am going pretty well as i just saw that if the user puts an invalid input at set /p, like an empty answer (just pressing enter) or an answer which is not on the "IF", the batch just crashes, and I would like to fix that so it will be less crashy.
Here is one of the parts i'd like to fix:
#echo off
title "Wasteland Adventure"
color 0A
cls
:Menu
cls
echo.
echo.
echo Welcome to Wasteland Adventure
echo.
echo To start a new game, type NEW and press ENTER.
echo To see instructions for the game, type INSTRUCTIONS and press ENTER.
echo To quit, type QUIT and press ENTER.
set input=
set /p input=What do you want to do?
if %input%==new goto INTRO
if %input%==instructions goto INSTRUCTIONS
if %input%==quit goto EXIT
Thanks in advance
it's not the set /pthat crashes, but:
if %input%==new
if %input% is empty, this is parsed as:
if ==new
obviously a syntax error. To avoid this, use:
if "%input%"=="new"
An empty input will then be parsed as:
if ""=="new"
which works fine.
The same applies when the variable contains only spaces and/or tabs:
if == new (syntax error) versus if " " == "new" (running fine)
Complete code like this:
:Menu
set input=
set /p input=What do you want to do?
if "%input%"=="new" goto INTRO
if "%input%"=="instructions" goto INSTRUCTIONS
if "%input%"=="quit" goto EXIT
REM for any other (invalid) input:
goto :Menu
Now i know how to create what i call tags
for example :startgame
And i know goto :startgame or in some occasions just goto startgame will take you back however
when i am doing if %selector% == r goto :Start_1
it just simply will close the batch file and i have tried with caps and without and without the :.
Now don't just be rude and call me ignorant i know basic batch
This is my start code
:Start_1
echo **************************************************
echo ************App selector by michaelukz************
echo **************************************************
echo **************************************************
echo To select an app press any button.
pause
This is the code i am trying to get to work
:Selected
echo You have selected your file.
echo If you wish to choose another file press R.
echo If you wish to close a program / file press C.
echo If you wish to close press any button.
SET /P"selected=Input letter here: "
if %selector% == r goto :Start_1
if %selector% == R goto :Start_1
if %selector% == c start taskmgr.exe
if %selector% == C start taskmgr.exe
if not timeout /T 3
echo Going to close menu
goto Closemenu
all the rest works except goto start_1.
Please help but don't be ignorrant - i have seen other people on here acting snarky as such.
A few things...
What you are calling tags are usually referred to as labels.
In your code, you are setting a variable called selected in this line SET /P"selected=Input letter here: ", but in your IF statements you are comparing to a variable called selector.
The line if not timeout /T 3 will not work. As #Magoo pointed out in a comment to the question, the syntax of if requires a comparison operator if [not] something compare-op something_else dothis.
take a look at CHOICE command, also you misspelled selector var name in SET /P
#echo off
:Start_1
echo **************************************************
echo ************App selector by michaelukz************
echo **************************************************
echo **************************************************
echo To select an app press any button.
:Selected
echo You have selected your file.
echo If you wish to choose another file press R.
echo If you wish to close a program / file press C.
echo If you wish to close press Q.
choice /C RCQ /N /M "Choose wisely [R,C,Q]" /D Q /T 30
goto action%errorlevel%
:action1
echo option R
goto start_1
goto selected
:action2
echo option C
start taskmgr.exe
goto selected
:action3
echo option Q
goto closemenu
goto Closemenu
I noticed a post on how to make a batch that opens another batch upon an input. However, I need a way that will make the code pause until the correct input is entered, then continue.
So for example, the code would ask what is the access code?
Then the user would input the correct code, for example 123.
Then the code would say "Welcome!"
Then it would execute another question like "What do you want to do today?"
There would be a list of options:
A. Game
B. Browse
C. Nevermind
The user would in put a, b, or c and the script would start either a game or web browser. If the user selected C, then it would exit.
Thanks for the help!
#echo off
echo Welcome!
echo What do you want to do today?
echo.
echo A. Game
echo.
echo B. Browse
echo.
echo C. Nevermind
echo.
choice /C:ABC /N /M "Enter your choice: "
if "%errorlevel%"=="1" goto :game
if "%errorlevel%"=="2" goto :browse
if "%errorlevel%"=="3" goto :nevermind
goto :error
I think a little bit modified version of code should work just fine.
#Echo off
:Start
cls
echo Welcome !
echo To Greet press one.
echo For Goodbye press two.
echo To Abort press 3
ECHO.
ECHO.
SET /p Option=Choice:
if "%Option%"=="1" GOTO Sub_MenuA
if "%Option%"=="2" GOTO Sub_MenuB
if "%Option%"=="3" GOTO Sub_MenuC
if "%Option%"=="quit" GOTO EOF
Goto Start
:Sub_MenuA
echo Hi There!
pause
Goto Start
:Sub_MenuB
echo tatas !
pause
Goto Start
:Sub_MenuC
echo Aborted
Pause
Goto Start
:EOF