Bat User Selection - batch-file

This is a simple question that I haven't been able to find an answer to. I am running a script that requires user input/selection. Here is the problematic area:
echo W -- WATCH_SCALE
echo E -- EARRING_SCALE
echo R -- RING_SCALE
echo B -- BOX
set /P rnFunc="choose a script: "
for %%I in (W E R B x) do if #%rnFunc%==#%%I goto assign%%I
The script works if user inputs the correct letter, however if user inputs an undefined letter such as 'T' the script continues on to the first option instead of breaking. I would like this to only work if the user inputs a W, E, R or B. What would my best option be?
Thanks so much.

You may want to take a look at the CHOICE command instead.
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List: /C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE: The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice listed
returns a value of 1, the second a value of 2, and so on. If the user
presses a key that is not a valid choice, the tool sounds a warning
beep. If tool detects an error condition, it returns an ERRORLEVEL
value of 255. If the user presses CTRL+BREAK or CTRL+C, the tool
returns an ERRORLEVEL value of 0. When you use ERRORLEVEL parameters
in a batch program, list them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."

Adding a goto end right after the for statement. Then make a label called end after all the other labels should solve your problem. Basically the first encountered label is being executed since it failed the for statement.

Related

How do I do that if you don't put anything in (IF) and press enter it will run correctly?

I wat that if user doesn't enter nothing or just type anything else that is not Y or N to ask him again.
I tried may things. I am realy new on batch so i need help.
(Nothing) is the enter or anything else that you put that is not Y or N
set /p again=Repeat again? [Y/N]=
if /i %again%== (Nothing) goto second
if /i %again%== N exit
if /i %again%== Y goto again
:second
set /p again=Repeat again? [Y/N]=
if /i %again%== (Nothing) goto second
if /i %again%== N exit
if /i %again%== Y goto again
Actualy if you just enter or type anything else that is not Y or N will ask one more time and will exit.
The issue you're having is because you're using the incorrect command for your task. When you use Set /P the end user is free to enter nothing or anything at all, (which includes poison characters). The solution is to use the correct command, Choice, which allows only specific entries. To find out exactly how to use the Choice command, open a Command Prompt window and enter choice /?.
Here's how I may do it using the Choice command.
In a batch-file:
Choice /C yn /N /M "Attack again [Y/N]="
If ErrorLevel 2 Exit /B
GoTo again
If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'
SET /P "var=Prompt" does not change var if Enter alone is pressed. Consequently, if var is originally empty, it remains empty.
if /i "%var%"=="value" performs a comparison on empty variables/values or those containing separators (eg spaces) The '/i' make the comparison case-insensitive.
IF DEFINED var is true if var is currently defined (note: not %var%.)
The syntax SET "var=value" (where value may be empty; in which case var becomes undefined) is used to ensure that any stray trailing spaces are NOT included in the value assigned.
In your code, the label again is undefined although the variable again may be defined (depending on user-input).
If you enter some value that is neither y nor n nor empty then your code will simply proceed to the next statement, so for instance if you enter z at the first set/p then execution will simply proceed through all of the if tests to the label second and execute the second set /p command.
For example:
:: Ensure the variable "again" is not defined
:RETRY1
set "again="
:: Get user-input to "again"
set /p again=Repeat again? [Y/N]=
:: Test the value entered - ENTER only (first way)
if defined again (echo something was entered) else (echo ENTER only)
:: Test the value (second way - more conventional using /i to make case-insensitive)
if "%again%"=="" goto second
if /i "%again%"=="N" exit
if /i "%again%"=="Y" goto yes
:: We get here if the entry was neither ENTER alone, "Y" or "N"
echo "%again%" is not a valid response.
goto retry1

Specific Keystroke for batch

I am trying to find out how to make batch wait for a specific keystroke. I don't want any of those pause>nul echo Press Enter to continue... None of that stuff, I would like it so that way once you press a key (eg. a) then it will go to :Test, if you press another key then it won't react what so ever. And I looked at the first page result, and went to the links on Google.
I believe the solution to your problem is the CHOICE command. This will allow you to set a key, have the batch script wait for that key to be pressed, then continue. It also avoids any cryptic pausing or loops in the script.
Keep in mind that the choice command has a few syntax's
/C - Specifies the list of choices to be created.
/N - Hides the list of choices in the prompt.
/CS- Enables case-sensitive choices to be selected.
/T - The number of seconds to pause before a default choice is made.
/D - Specifies the default choice after timeout seconds.
/M - Specifies the message to be displayed before the prompt.
For the sake of your usage, the following script can be used. Its also important to note you can have multiple keys act as your specific keystroke by doing ex: 12345 for SET KEY=
#ECHO OFF
::Change this key to what ever you wish.
SET KEY=N
:Start
cls
echo Press %KEY% to continue...
choice /c %KEY% /n /cs
if %errorlevel%==1 goto Continue
:Continue
cls
echo Hello World!
pause.

Detecting a keyboard input in batch

In a batch file im messing with, I want the program to goto a certain section after pressing a certain key, such as D. Is there any way to do this after pressing a key without also having to press enter? I'm sorta a beginner.
This is possible using the choice command. This command will wait for a keypress, and if the key is in the list of keys after /c then it returns the offset of the key pressed in the list of keys.
choice /c AB /m "Press A or B"
if errorlevel 2 goto optionb
if errorlevel 1 goto optiona
You can also use the optional /n arguement to hide the list of options ([A,B] in this case) that appears after the message text.

Terminate current command and move to next command in bat file

I've created a batch file
#echo off
Mkdir c:\asp
Copy * c:\asp\
Cd/
CD c:\asp
Dir
Pause
If I want to terminate the copy command and move to dir command how can I do so?
Since CTRL + C will terminate overall script and not a single command , I suggest to launch Copy command in a separate CMD window without /WAIT and then use label to go to your desired command ,
#echo off
Mkdir c:\asp
START Copy * c:\asp\
GOTO SABIRLABEL
Cd/
CD c:\asp
:SABIRLABEL
Dir
Pause
You can then use CTRL + C in second window to terminate copy if its long running process.
Also you can make GOTO conditional on some variable value if you don't always wish to skip those two commands.
Hope it helps !!
I think you could use "CHOICE" (available under DOS in Win 8.1)
#echo off
Mkdir c:\asp
HERE goes CHOICE///// etc...
Copy * c:\asp\
Cd/
CD c:\asp
Dir
Pause
Example:
OPEN DOS BOX...
TYPE CHOICE /?
See this...
C:\Windows\system32>choice /?
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."

batch file - 'The Syntax of the command is incorrect'

I've been making a text-adventure, but towards the end of my coding it came up with this error. Every time I try to continue from scene 5 to scene 5.1 it goes to a different scene and skips through it. Help?
Here is my code:
cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1
cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1
:scene5.1
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
echo 1) Table
echo 2) Sofa
echo 3) Fireplace
echo 4) Go back
set /p type
if %type%==1 goto scene5.1.1
if %type%==2 goto scene5.1.2
if %type%==3 goto scene5.1.3
if %type%==4 goto scene5.1.4
C:\Windows\system32>echo 4) Go back
4) Go back
C:\Windows\system32>set /p type
The syntax of the command is incorrect.
goto was unexpected at this time.
It's probably the lack of an equals sign in set /p (well I know it is).
PS: Set /p var = PromptText is not the best command. It doesn't prevent wrong entry, and you don't check for it. Use Choice command instead. It only takes allowed entry.
The reason most examples use set /p is because choice was dropped from one windows version 15 years ago. That was along time ago.
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
As help says use errorlevels in descending order
if errorlevel 4 dosomething
if errorlevel 3 dosomething
if errorlevel 2 dosomething
if errorlevel 1 dosomething
if errorlevel 0 UserPressedCtrl+C
or in any order
if errorlevel 1 if not errorlevel 2 dosomething for 1
Do not use %errorlevel% as it can be overwritten by other programs.
#ECHO OFF
SETLOCAL
:scene4.6.1
:scene5.1
SET "scene=5.1"
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
CALL :addchoice Table Sofa Fireplace "Go back"
GOTO makechoice
:scene5.1.1
ECHO AT table
goto :eof
:scene5.1.2
ECHO AT sofa
goto :eof
:scene5.1.3
ECHO AT fireplace
goto :eof
:scene5.1.4
:scene4.6
SET "scene=4.6"
echo You are IN a dank corridor dimly lit by sputtering torches
echo.
CALL :addchoice Doorway Corridor
GOTO makechoice
:makechoice
CHOICE /C %choices%
SET "scene=%scene%.%errorlevel%"
SET /a choicecount=0
SET "choices="
GOTO scene%scene%
:addchoice
SET /a choicecount+=1
ECHO %choicecount%) %~1
SET "choices=%choices%%choicecount%"
SHIFT
IF "%~1" neq "" GOTO addchoice
GOTO :EOF
This may save you a heap of programming. Please do not accept it as an answer since bgalea's answer actually answers your question. This is just an approach to make the construct-an-adventure exercise faster and easier.
For each scene, describe the scene and call :addchoice with parameters of the available choices. If a choice is multiple words, "enclose them in quotes".
The routine assigns 1 to the first choice in the list, 2 to the next and so on. The available choices are then recorded in choices.
go to makechoice. This prompts for an entry using the list in choices. Then append dot+the errorlevel of the response to the current scene number, clear the choices and goto scene+the calculated scene number.
Note that you can use an empy command-sequence to move between scenes by using the structure like in scene5.1.4 to scene4.6 (iow, scene 5.1+choice4 moves to 4.6)
This way, your response-sets become one line after your scene description and you never need to use a series of if-commands to move between scenes.

Resources