Store a sentence in a variable (batch) - windows

I am making a batch file, and whenever their is a user input field, which the user enter a word or a sentence and it get stored in a text file. I made all the job done.
But when the user's input is more than 1 word, it gives the following error:
*second word* was unexpected.
How can this be solved?
#echo off
color f0
:menu
cls
echo Batch Editor
echo Choose what you want to do:
echo 1) New Document
echo 2) Load existing document
echo 3) Learn How to Use
echo 4) Exit
set /p menu=
if %menu%==1 goto new
if %menu%==2 goto load
if %menu%==3 goto learn
if %menu%==4 goto exit
if [%1]==[] goto learn
echo Invaild Code
pause
goto menu
:new
cls
echo You have chosen to start a new document.
echo But please note that is impossible to know how to code
echo without seeing the "How to Learn" section.
echo Choose:
echo 1) Continue anyways
echo 2) Go to the "How to Learn" section
echo 3) Exit
set /p type=
if '%type%'=='' goto new
if %type%==1 goto newdoc
if %type%==2 goto learn
if %type%==3 goto exit
echo Invaild Key
pause
goto new
:newdoc
cls
set /p input=
if %input%==Qsave goto saving
if %input%==qsave goto saving
if %input%==qSave goto saving
if %input%==QSAVE goto saving
goto newdoc1
pause
:newdoc1
set /p chosen=
if '%chosen%'==''(
goto newdoc2)
else if "%chosen%"=="Qsave" (
goto saving)
else if "%chosen%"=="qsave" (
goto saving)
else if "%chosen%"=="qSave" (
goto saving)
else if "%chosen%"=="QSAVE" (
goto saving)
else (
goto newdoc2)
:newdoc2
set /p chosen=
if '%chosen%'==''(
goto newdoc1)
else if %chosen%==Qsave (
goto saving)
else if %chosen%==qsave (
goto saving)
else if %chosen%==qSave (
goto saving)
else if %chosen%==QSAVE (
goto saving)
else (
goto newdoc1)
:saving
cls
echo Saving
pause
:load
echo LOADING
pause
:learn
echo Learn
pause
:exit
cls
set /a exiting=0
echo Are you sure you want to exit?
echo Choose:
echo 1)Exit
echo 2)Go Back to the Main Menu
set /p exiting=
if %exiting%==1 exit
if %exiting%==2 goto menu
echo Invaild key
pause
goto exit
pause
exit

You really should post what you currently have, if all you're trying to do is store user inputted text to a text file, something like this might help?
SET /P Answer=Say somthing witty!
ECHO %Answer%>Response.txt

set var=Alpha beta Gamma
if %var%==whatever
is translated to:
if Alpha beta Gamma==whatever
IF Syntax is IF <Argument1> <comparator> <Argument2> <command>
Argument1 is Alpha, Comparator is beta - wait, this is wrong...
It leads to "beta was unexpected", because there should be a compatator (==), not an Argument.
Better Syntax:
if "%var%"=="whatever"
is translated to:
if "Alpha beta Gamma"=="whatever"
Argument1 is "Alpha beta Gamma", Comparator is ==, Agrument2 is "whatever" - fine Syntax.

Sample menu where there are only three possible choices plus one covering mistype:
#echo off
:retry
cls
Unset the variable M
set M=
If nothing typed, then set M to 4
SET /P "M=Type 1 for pizza, 2 for soup or 3 for exit, then press ENTER: " || set "M=4"
Here we use variable substring. See http://ss64.com/nt/syntax-substring.html
IF [%M:~0,1%]==[1] goto pizza
If M are not set to 1, then go to the next line, etc...
IF [%M:~0,1%]==[2] goto soup
If M are not set to 1 or 2 then check if it's set to 3, there is the last IF statement, we can now use else to cover everything else in the variable M
IF [%M:~0,1%]==[3] (
echo Bye...
#timeout /T 1 /nobreak >NUL
exit /b 0
) else (
goto :retry
)
:pizza
echo Pizza
goto :EOF
:soup
echo Soup
goto :EOF
Edit: My if statement was a bit redundant, see the revision history to see what I mean.

Related

How do I silence a batch script file

Okay so I am trying to make a rpg and it's going good so far but I want to fix this one bug. I use a system so when you press "w s d" It will go down up or select. The problem is that when I press a difrent button like "Q" or "K" it will beep at me.
I have been stumped with this for a while now.
:startgame
cls
if %select% gtr 4 set select=1
if %select% lss 1 set select=4
set s1=-
set s2=-
set s3=-
set s4=-
set s%select%=#
echo --------------------------------
echo Welcome to StormTides.
echo --------------------------------
echo Version: v1.1: Beta_Test
echo What would you like to do?
echo.
echo [%s1%] Load Save
echo [%s2%] New Save
echo [%s3%] Controls
echo.
echo --------------------------------
echo [%s4%] Exit
if "%msplash%"=="y" echo.
choice /c:wsd /n /m ""
set msplash=n
if "%errorlevel%"=="1" set /a select-=1
if "%errorlevel%"=="2" set /a select+=1
if "%errorlevel%"=="3" (
if "%select%"=="1" set select=1&goto login2
if "%select%"=="2" set select=1&goto createuser
if "%select%"=="3" set select=1&goto controls
if "%select%"=="4" exit
)
goto startgame
Your choice command only allows w, s or d. If you want to allow a user to enter other chars like Q or K, you need to add them to the choice command ... for example, choice /c:wsdQK ...

If statement in batch file - user chooses option

New to batch/sql files. Thanks for you help.
What im trying to do:
user picks option 1, runs my sql file.
user picks 2, exits program.
user enters nothing or invalid option, ECHO "invalid option".
Problem: anything I type will exit my program. What am I doing wrong?
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :sql file
) ELSE (
IF "%input%"=="2" (
GOTO :Exit
)
) ELSE (
IF "%input%"=="" (
ECHO Invalid option chosen.
)
)
PAUSE
Just use the choice command, its instructions are available by entering choice /? at the Command Prompt.
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
CHOICE /C 12 /M "Your choice"
IF ERRORLEVEL 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
If you still wanted to use, what in your case I believe is the wrong Set /P input method, then something like this should be relatively robust, given that your end user is free to enter whatever they like as input, mischievous or harmful intent included:
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
:GETInput
SET "input="
SET /P "input=Your choice: "
2>&1 SET input | FINDSTR /XR "input=[12]" 1>NUL || (
ECHO Invalid option chosen
GOTO GETInput
)
IF %input% EQU 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
There is one extract closing bracket [Line #12] inside nested if..else. Also, as far as i know, your label :sql file should not contain space and you must declare it inside the batch file (Same for label :Exit)
I did few changes in your code to make it work
#echo off
:Main
cls
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :SQLReport
) ELSE IF "%input%"=="2" (
GOTO :EOF
) ELSE (
ECHO.
ECHO Invalid option chosen.
pause
GOTO :Main
)
:SQLReport
ECHO.
ECHO Running SQL Report...
GOTO :EOF
:EOF

Batch: simple game syntax is wrong, can't figure out why?

The game code is pretty simple, the code for it is below. Can't figure out why syntax for :WRONG section is well wrong. I've tried mirroring it exactly from the start section which works and is identical. Anyone offer any input?
color 4a
cls
:MAIN
#echo off
echo Welcome to XXXX's Trivia Game!!
pause & echo Ready to start?
set /p input=(Y/N)
if %input%==y goto SECTION1
goto BYE
:BYE
#echo Sorry to see you leave! Have a nice day!
pause
exit
:SECTION1
#echo FIRST QUESTION!!!!
pause
cls & echo At what temperature does rain turn to snow?
echo A. 112 degrees
echo B. -37 degrees
echo C. 32 degrees
echo D. 17,341 degrees
set /p input=Answer?
if %input%==C goto 2
goto WRONG
:SECTION2
:SECTION3
:SECTION4
:SECTION5
:SECTION6
:SECTION7
:SECTION8
:SECTION9
:SECTION10
:WRONG
cls & echo You were wrong, would you like to start again?
set /p input==(Y/N)
if %input%==y goto :SECTION1
goto BYE
pause
:HALFWAY
:WIN
Things I've tried:
Removing all sections between :SECTION1 and :WRONG
Renaming :WRONG and the call for it in :SECTION1
Removing :HALFWAY and :WIN
Copying from :MAIN (working correctly) to :WRONG (still no dice)
The following command has an extra = that causes a syntax error:
set /p input==(Y/N)
The prompt for set /p cannot begin with =. Remove the unwanted char as follows, and the command will work.
set /p input=(Y/N)
You are reusing the input variable, and set /p will preserve any pre-existing value if the user simply presses <Enter>. You should clear the value before prompting for input.
set "input="
set /p input=(Y/N)
There are lots of additional areas where your code could (should) be improved, but I will let you discover them at your own pace.
A basic yes/no choice command example for you:
#Choice /M "Ready to start?"
#Echo=%%ERRORLEVEL%% is %ERRORLEVEL%
#Timeout 5
Run it and see what happens when you enter different options.
Fixed your game :)
Try the below code...
Edit: Did some code improvements and cleanup, hope you'd like it...
#echo off
color 4a
cls
:MAIN
cls
echo.
echo --------------------------------------
echo Welcome to New-B-Admin's Trivia Game!!
echo --------------------------------------
echo.
set /p input="Ready to start playing ? Answer (Y/N): "
if /i "%input%"=="y" goto SECTION1
if /i "%input%"=="n" goto BYE
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto MAIN
:SECTION1
cls
echo ------------------
echo Question Number 1
echo ------------------
echo.
echo At what temperature does rain turn to snow?
echo.
echo A. 112 degrees
echo B. -37 degrees
echo C. 32 degrees
echo D. 17,341 degrees
echo.
set /p input="Your answer?: "
if /i "%input%"=="a" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="c" (
call :CORRECT
goto SECTION2
)
if /i "%input%"=="d" goto WRONG
::the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION1 rem rename this goto statment to the same name as the label.
:SECTION2
cls
echo ------------------
echo Question Number 2
echo ------------------
echo.
echo What year was the two dollar bill last printed in the United States?
echo.
echo A. 1997
echo B. 2001
echo C. 2003
echo D. 2007
echo.
set /p input="Your answer?: "
if /i "%input%"=="a" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="c" (
call :CORRECT
goto SECTION3
)
if /i "%input%"=="d" goto WRONG
:: the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION2 rem rename this goto statment to the same name as the label.
:SECTION3
cls
echo ------------------
echo Question Number 3
echo ------------------
echo.
echo How many super bowls have the Denver Broncos won?
echo.
echo A. 5
echo B. 10
echo C. 7
echo D. 3
echo.
set /p input="Your answer?: "
if /i "%input%"=="a" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="d" (
call :CORRECT
goto SECTION4
)
if /i "%input%"=="c" goto WRONG
:: the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION3 rem rename this goto statment to the same name as the label.
:SECTION4
cls
echo ------------------
echo Question Number 4
echo ------------------
echo.
echo What was the name of the hourse from The Lone Ranger Movie that he saved from an enraged buffalo?
echo.
echo A. Silver
echo B. Yellow
echo C. White
echo D. Buck
echo.
set /p input="Your answer?: "
if /i "%input%"=="c" goto WRONG
if /i "%input%"=="b" goto WRONG
if /i "%input%"=="a" (
call :CORRECT
goto SECTION5
)
if /i "%input%"=="d" goto WRONG
:: the below if statment prevents the user from just hitting Enter Key or any non relevant letters or numbers.
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto SECTION4 rem rename this goto statment to the same name as the label.
:SECTION5
:SECTION6
:SECTION7
:SECTION8
:SECTION9
:SECTION10
:END
cls
echo.
echo --------------------
echo No more questions!!!
echo --------------------
echo.
set /p input="Would you like to restart the game ? Answer (Y/N): "
if /i "%input%"=="y" goto SECTION1
if /i "%input%"=="n" goto BYE
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto END
:WRONG
cls
echo.
set /p input="You were wrong, would you like to start again?(Y/N): "
if /i "%input%"=="y" goto SECTION1
if /i "%input%"=="n" goto BYE
if not "%input%"=="Place_Holder_Value_Dont_Remove" goto WRONG
:CORRECT
cls
set input=0
echo.
echo Correct!!! Great progress :)
echo Press enter to continue to next question...
pause >nul
goto :EOF
:HALFWAY
:WIN
:BYE
cls
echo.
echo ----------------------------------------
echo Sorry to see you leave! Have a nice day!
echo ----------------------------------------
pause >nul
exit

Checking Blank User Input in Batch file (No Input) [duplicate]

This question already has an answer here:
set /p empty answer crash
(1 answer)
Closed 7 years ago.
So, I am asking the user to make an selection. The point of this question is to catch if the user hit just entered without any input. My plan is to find out if the user entered nothing and just hit entered. If so, then display that it is not a valid option and go back to :START. The following code does not work - especially the empty input and the input with just space character. If the user did this invalid thing then I just want to display the message saying it is empty.
Any suggestions on how to handle this issue would be greatly appreciate.
#echo off
:START
cls
echo Choose your option & echo.
echo [P] play
echo [R] rules
echo [M] main menu
echo [E] exit
set /p "cho=->"
if %cho%==e (goto EXIT
) else if /I %cho%==m (goto MAINMENU
) else if /I %cho%==r (goto GAMERULES
) else if /I %cho%==p (goto GAMERULES
) else if %cho%=="" (goto EMPTYINPUT
) else if %cho%==" " (goto EMPTYINPUT
) else cls
echo That is not valid
pause
:EMPTYINPUT
echo This is not a valid option.
pause
goto START
You may avoid all the option identification problems if you use choice instead set /P. Choice command will respond just to a limited set of keys defined by you, so its answer is always correct.
#echo off
:START
cls
echo Choose your option & echo.
echo [P] play
echo [R] rules
echo [M] main menu
echo [E] exit
choice /C PRME /N /M "->"
goto option-%errorlevel%
:option-1 PLAY
echo In play
pause
goto start
:option-2 GAMERULES
echo In game rules
pause
goto start
:option-3 MAINMENU
echo In main menu
pause
goto start
:option-4 EXIT
echo Bye...
goto :EOF
For further details, type: choice /?
You can also try it with a dynamic menu like this :
#echo off
Mode con cols=90 lines=15
:menuLOOP
Mode con cols=70 lines=15
Cls & color 0B
Title Example of Dynamic Menu
echo(
echo( ============================Menu===========================
echo(
for /f "tokens=2* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo( %%A %%B
echo(
echo( ============================================================
set choice=
echo( & set /p choice=Make a choice or hit ENTER to quit: || GOTO :EOF
echo( & call :menu_[%choice%]
GOTO:menuLOOP
:menu_[P] PLAY
cls & Color 0A
echo.
echo In play
pause
GOTO:menuLOOP
:menu_[G] GAMERULES
cls & Color 0C
echo.
echo In game rules
pause
GOTO:menuLOOP
:menu_[M] MAINMENU
cls & Color 0D
echo.
echo In main menu
pause
GOTO:menuLOOP
:menu_[E] EXIT
echo Bye...
Exit

Goto was unexpected at this time batch windows 7 starter

This code is designed to resemble a simpler version of the Pokemon battle gameplay. I've only coded in the attacks. I've been testing thoroughly, and found that an error message (Goto was unexpected at this time) whenever the user confirmed their attack. WARNING!! Code is 96 lines long. At the end, I'll put the problem section, so you can skip this first huge chunk.
#echo off
Set H1=20
Set A1=8
Set D1=6
Set S1=5
Set H2=14
Set A2=5
Set D2=4
Set S2=8
:Begin
CLS
Echo Bulbasur
Echo %H2%/14 /\
Echo (__) ___
Echo l __lo.ol
Echo l_\ l_\"
Echo.
Echo _
Echo * / \
Echo \\l )
Echo \\__l Charmander
Echo %H1%/20
Echo -Attack -Capture
Echo -Item -Run
Set /p Move=Action?
If %move%==Attack goto Attack
If %move%==Catpure goto capture
If %move%==Item goto Item
If %move%==Run Goto Run
Echo I'm sorry, Charmander can't do that.
Pause
goto Begin
:Attack
ClS
Echo Attacks
Echo 1)Tackle
Echo 2)Growl
Echo 3)Ember
Echo 4)Scratch
Set /p attack=Which one?
If %attack%==Tackle goto Tackle
If %attack%==1 goto Tackle
If %attack%==Growl Goto Growl
If %attack%==2 goto Growl
If %attack%==Ember goto Ember
If %attack%==3 goto Ember
If %attack%==Scratch goto Scratch
If %attack%==4 goto Scratch
If %attack%==Cancel goto Begin
Echo I didn't get that
Goto Attack
:Tackle
CLS
Echo Tackle Hits The opponent where it hurts. EVERYWHERE.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Tackle
:Growl
CLS
Echo Growl lowers the opponents attack.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Status
If %acccept%==No goto Begin
Echo I didn't get that.
goto Growl
:Scratch
CLS
Echo Scratch hits the foe with a claw.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Scratch
:Ember
CLS
Echo Ember hits the opponent with a small fire.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Ember
:Combat
CLS
If NOT %attack%==Growl If NOT %attack%==2 set /a H2=%H2%-(%A1%^2/%D2%)
set /a H1=%H1%-(%A2%^2/%D1%)
goto Begin
:Status
CLS
Set /a A1=%A1%-1
goto Combat
Problem Area:
:Tackle
CLS
Echo Tackle Hits The opponent where it hurts. EVERYWHERE.
Echo Do you want to?
set /p accept=Yes/No?
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Echo I didn't get that.
goto Tackle
The code gets here fine, but once I'm here, it doesn't expect the goto commands. Anyone can fix this beef? (Note: Tackle is just an example. None of the attacks work.) EDIT: If the user puts in "Yes","No",gibberish, or nothing, it still delivers the same error message (goto was unexpected at this time)
You have to put it in quotes:
if "%accept%"=="yes" goto combat
if "%accept%"=="no" goto begin
Or rather if you dont want to make it case sensitive:
if /i "%accept%"=="yes" goto combat
if /i "%accept%"=="no" goto begin
Your problem is that this line:
set /p accept=Yes/No?
does NOT use the same variable name that these ones:
If %acccept%==Yes goto Combat
If %acccept%==No goto Begin
Above variables have "ccc", but the first one just "cc"
EDIT
Hi, user1205760; I got some time to spend, so I take your program and made it somewhat smaller. This is my version:
#echo off
Setlocal EnableDelayedExpansion
Set Actions=Attack Capture Item Run
Set Attacks=Tackle Growl Ember Scratch Cancel
Set i=0
For %%a in (%Attacks%) do set /A i+=1 & set Attack[!i!]=%%a
Set H1=20
Set A1=8
Set D1=6
Set S1=5
Set H2=14
Set A2=5
Set D2=4
Set S2=8
:Begin
:Cancel
CLS
Echo Bulbasur
Echo %H2%/14 /\
Echo (__) ___
Echo l __lo.ol
Echo l_\ l_\"
Echo.
Echo _
Echo * / \
Echo \\l )
Echo \\__l Charmander
Echo %H1%/20
Echo -Attack -Capture
Echo -Item -Run
Set /p Move=Action?
For %%a in (%Actions%) do if /I %move%==%%a goto %move%
Echo I'm sorry, Charmander can't do that.
Pause
goto Begin
:Attack
Cls
Echo Attacks
For %%a in (1 2 3 4) do echo %%a)!Attack[%%a]!
Set /p attack=Which one?
for %%a in (1 2 3 4) do if %attack%==%%a set attack=!Attack[%%a]!
for %%a in (%Attacks%) do if /I %attack%==%%a goto %attack%
Echo I didn't get that
Pause
Goto Attack
:Tackle
call :Confirm Tackle Hits The opponent where it hurts. EVERYWHERE.
If %accept%==Yes goto Combat
goto Begin
:Growl
call :Confirm Growl lowers the opponents attack.
If %accept%==Yes goto Status
goto Begin
:Ember
call :Confirm Ember hits the opponent with a small fire.
If %accept%==Yes goto Combat
goto Begin
:Scratch
call :Confirm Scratch hits the foe with a claw.
If %accept%==Yes goto Combat
goto Begin
:Status
Set /A A1-=1
:Combat
If /I NOT %attack%==Growl set /A H2=H2-(A1^2/D2)
set /A H1=H1-(A2^2/D1)
goto Begin
:Confirm
Cls
Echo %*
Echo Do you want to?
set /p accept=Yes/No?
For %%a in (Yes No) do if /I %accept%==%%a exit /B
Echo I didn't get that.
Pause
goto Confirm
If the user enters nothing, your If lines probably evaluate to something like this:
…
If ==Yes goto Combat
If ==No goto Begin
…
which is syntactically incorrect. I would suggest initialising accept before the set /p command with some default value:
…
set accept=default
set /p accept=Yes/No?
if …
That way, if the user just hits Enter, the accept variable will retain the default value, and the subsequent if will not end up in the error.
Excuse me,
This is just a question to the downvotes.
The people who asks just IS NOT SURE must the problem be in THAT PART.
e.g.
set a=
if %a%==1 echo yes
If I just post this line:
if %a%==1 echo yes
Then WILL EVERYBODY KNOW WHAT'S THE PROBLEM?
Remember for variable, like %abc%, it's better to use it with ",[ or { so as to prevent error message.
e.g.
set /p abc=
and the user inputs nothing.
Then the next line should be:
if %abc%==1 echo Hi
But it became:
if ==1 echo Hi
,as "%abc%"==""
But with "", it will become
if ""=="1" echo Hi
And "" unequal to "1".
Understand?
EDIT---
If you're using Windows 7 ( or other versions ), you may also try this:
choice /c YN /n /m "Confirm? [Y^|N]
The ^ is just escaping the "pipe" (|).
Hope this will be useful to you!

Resources