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.

a

GPIA7RGPIA7R Registered User regular
edited August 2017 in Help / Advice Forum
.

GPIA7R on

Posts

  • embrikembrik Registered User regular
    edited September 2008
    I'm not quite sure... any chance you could use vbscript instead?

    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?
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • Sir Headless VIISir Headless VII Registered User regular
    edited September 2008
    You cant just input the two words as 2 variables and only check against the first?


    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

    Sir Headless VII on
    Steam - Backpack - Bnet: SirHeadless #1154
    7KEFduI.jpg
  • aperlscriptaperlscript Registered User regular
    edited September 2008
    Here's one way:
    set /p command=
    call :parse %command%
    
    
    :parse
    if /i "%1" == "slap" (
        echo slapped
    ) else ( 
        echo not slapped
    )
    exit /b
    

    aperlscript on
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • DaenrisDaenris Registered User regular
    edited September 2008
    Well, to see the error message you should be able to just run the batch script from a command line. It won't close the window after exiting then.

    what aperlscript shows should work. I just tested the following script:
    @echo off
    set /p command=
    call :parse %command%
    
    :parse
    if /i "%1" == "slap" (echo slapped "%2") else (echo not slapped)
    
    exit /b
    
    And it correctly outputs the second word when I enter slap, so you can do something based off that word.

    Daenris on
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • DaenrisDaenris Registered User regular
    edited September 2008
    GPIA7R wrote: »

    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.

    Daenris on
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • DaenrisDaenris Registered User regular
    edited September 2008
    Why don't you show us your batch?

    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.

    Daenris on
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • GPIA7RGPIA7R Registered User regular
    edited September 2008
    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.

    GPIA7R on
  • BarrakkethBarrakketh Registered User regular
    edited September 2008
    GPIA7R wrote: »
    I dunno, there's just something so satisfying making a super-complex program from a batch file :)

    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.

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • DaenrisDaenris Registered User regular
    edited September 2008
    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.

    Daenris on
  • Sir Headless VIISir Headless VII Registered User regular
    edited September 2008
    and for the blank input thing testing against "" will work ie.

    if %command% == "" goto home2

    Sir Headless VII on
    Steam - Backpack - Bnet: SirHeadless #1154
    7KEFduI.jpg
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • DaenrisDaenris Registered User regular
    edited September 2008
    GPIA7R wrote: »
    GPIA7R wrote: »
    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.

    Daenris on
  • GPIA7RGPIA7R Registered User regular
    edited August 2017
    .

    GPIA7R on
  • embrikembrik Registered User regular
    edited September 2008
    Barrakketh wrote: »
    GPIA7R wrote: »
    I dunno, there's just something so satisfying making a super-complex program from a batch file :)

    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.

    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.
    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."

    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?
  • DaenrisDaenris Registered User regular
    edited September 2008
    %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.

    Edit2: edited for better way of parsing input.

    Daenris on
  • Sir Headless VIISir Headless VII Registered User regular
    edited September 2008
    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
    

    Sir Headless VII on
    Steam - Backpack - Bnet: SirHeadless #1154
    7KEFduI.jpg
Sign In or Register to comment.