The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
In my case, I want the "slap (something)" to take it to a separate :routine, so instead of echo I would put goto?
Correct
Also, what's the exit /b? Probably not needed in my case?
The exit /b will exit the parse subroutine and return to the original code at the next line. In this case there is actually no next line, so after it returns it just exits the batch script.
I've also never done anything with %1, %2, etc... will any of my other variables effect them?
%1, %2, etc represent the elements in the input. So when command is "slap blah" and we call the parse subroutine with an input parameter of command, %1 is slap, and %2 is blah. If command was "slap blah eh", we'd also have a %3 that was eh.
You will have to put the parse section as a subroutine in order to use the %1 and %2 variables. Then you can just use gotos in that subroutine to goto to the section you want.
Another discovery - Can I make it where a blank input WONT do the same thing (crash the batch)? I'd prefer if they hit "enter" at any of my prompts, it would just, y'know, blink... and repeat the prompt, rather than crash out.
First off, the if NOT lines seem crazy excessive. The way you have it now, if it is any valid command it's gone to some subroutine. So you really only need a single goto :badcommand after all the if statements.
Try something more like:
:home2
set /p command="ADMIN> "
call :parse %command%
:parse
if %1%==a call :a %2%
if %1%==b call :b %2%
if %1%==c call :c %2%
if %1%==slap call :slap %2%
goto badcommand
:a
stuff
:b
stuff
:c
stuff, and so on
:slap
if %1% == blah ...... etc/whatever you do here with the second word
Honestly, I can think of a number of languages that would make this much easier to complete rather than windows batch scripts.
and for the blank input thing testing against "" will work ie.
if %command% == "" goto home2
Didn't work =/
:home2
set /p command="ADMIN> "
if "%1"=="" goto home2
This worked.
Now to fix the multiple word input problem... :0 I think we're close... it has to be related
Did you try the suggestion I had above? In order to have the %1 and %2 values work correctly you need to first input the command, then call a subroutine with the command. By default, %1 and %2, etc should I believe just hold the command line parameters that were provided when calling the script, if any. By passing %command to a subroutine, that subroutine has %1, %2, etc, based on the contents of %command.
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."
embrik on
"Damn you and your Daily Doubles, you brigand!"
I don't believe it - I'm on my THIRD PS3, and my FIRST XBOX360. What the heck?
%1, etc represent the inputs to a given command or batch script. So if your batch script is called slaphappy.bat and you call it by going:
slaphappy.bat bob uncle
Then %1 will be bob and %2 will be uncle. This is why your if %1=="" pretended to work at first... because I'm assuming you call this batch script with no parameters, so %1 is in fact "".
In order to split up a command into multiple words, we can call a subroutine with the command as the input. So if %command is "slap bob" then when we call :parse and pass it %command as an input, it separates this into %1 = slap and %2 = bob.
So something like this works, though it doesn't seem to handle blank input for some reason.
@echo off
:home2
set /p command="ADMIN> "
FOR /F "tokens=1,2" %%A IN ("%command%") DO SET CHOICE=%%A & SET OPTION=%%B
if %CHOICE%==a call :a %OPTION%
if %CHOICE%==b call :b %OPTION%
if %CHOICE%==c call :c %OPTION%
if %CHOICE%==slap call :slap %OPTION%
if %CHOICE%==q exit /b
goto badcommand
:a
echo %1
echo.
goto home2
:b
echo %1
echo.
goto home2
:c
echo %1
echo.
goto home2
:slap
echo %1
echo.
goto home2
:badcommand
echo '%command%' not found
echo.
goto home2
Edit:... but again this and other things are going to be much easier in other languages. Windows batch scripting is a nightmare of seemingly random little quirks in dealing with most commands.
Just pressing enter seems to just keep whatever was in the variable before there. Its ugly but if you set the command to something no one will ever input just before you get it and test against that you can get it working. like this
edit: setting it to blank is a much better idea
@echo off
:home2
set command=""
set /p command="ADMIN> "
FOR /F "tokens=1,2" %%A IN ("%command%") DO SET CHOICE=%%A & SET OPTION=%%B
if %CHOICE%==a call :a %OPTION%
if %CHOICE%==b call :b %OPTION%
if %CHOICE%==c call :c %OPTION%
if %CHOICE%=="" goto home2
if %CHOICE%==slap call :slap %OPTION%
if %CHOICE%==q exit /b
goto badcommand
:a
echo %1
echo.
goto home2
:b
echo %1
echo.
goto home2
:c
echo %1
echo.
goto home2
:slap
echo %1
echo.
goto home2
:badcommand
echo '%command%' not found
echo.
goto home2
Posts
I don't believe it - I'm on my THIRD PS3, and my FIRST XBOX360. What the heck?
or you could do something like this
set /p input=
call test2.bat %input%
if %command2%==slap bob goto slap
with test2.bat as
set command2 = %1
But then you would need to create another batch file
what aperlscript shows should work. I just tested the following script:
And it correctly outputs the second word when I enter slap, so you can do something based off that word.
Correct
The exit /b will exit the parse subroutine and return to the original code at the next line. In this case there is actually no next line, so after it returns it just exits the batch script.
%1, %2, etc represent the elements in the input. So when command is "slap blah" and we call the parse subroutine with an input parameter of command, %1 is slap, and %2 is blah. If command was "slap blah eh", we'd also have a %3 that was eh.
You will have to put the parse section as a subroutine in order to use the %1 and %2 variables. Then you can just use gotos in that subroutine to goto to the section you want.
After seeing what you have at the bottom of the page, it isn't even super-complex. I would only call it tedious.
That would be so much simpler in any scripting language that has some version of a switch statement.
Try something more like:
Honestly, I can think of a number of languages that would make this much easier to complete rather than windows batch scripts.
if %command% == "" goto home2
Did you try the suggestion I had above? In order to have the %1 and %2 values work correctly you need to first input the command, then call a subroutine with the command. By default, %1 and %2, etc should I believe just hold the command line parameters that were provided when calling the script, if any. By passing %command to a subroutine, that subroutine has %1, %2, etc, based on the contents of %command.
Yeah, you could even use the DOS command "choice". It'll return whatever the user enters, and you use the variable ERRORLEVEL to handle the entry.
I don't believe it - I'm on my THIRD PS3, and my FIRST XBOX360. What the heck?
slaphappy.bat bob uncle
Then %1 will be bob and %2 will be uncle. This is why your if %1=="" pretended to work at first... because I'm assuming you call this batch script with no parameters, so %1 is in fact "".
In order to split up a command into multiple words, we can call a subroutine with the command as the input. So if %command is "slap bob" then when we call :parse and pass it %command as an input, it separates this into %1 = slap and %2 = bob.
So something like this works, though it doesn't seem to handle blank input for some reason.
Edit:... but again this and other things are going to be much easier in other languages. Windows batch scripting is a nightmare of seemingly random little quirks in dealing with most commands.
Edit2: edited for better way of parsing input.
edit: setting it to blank is a much better idea