• Batch File Assistance (XP)

    Author
    Topic
    #451575

    I am trying to write a simple batch file using “INPUT”. The challenge I am encountering is the MENU is flickering most likely due to lines of code in the batch file.

    Example Code:
    @ECHO OFF

    :MENU
    CLS
    ECHO 1. Display Time
    ECHO 2. Dispaly Date
    ECHO 3. Display current Directory
    ECHO Q to Quit

    set INPUT=
    set P INPUT=Please make a selection

    if “%input%”==”” goto MENU
    if “%input%”==”1” goto TIME
    if “%input%”==”2” goto DATE
    if “%input%”==”3” goto DIR
    if /I “%input%”==”Q” goto QUIT
    GoTo MENU

    :TIME
    TIME
    goto MENU

    :DATE
    DATE
    goto MENU

    :DIR
    DIR
    goto MENU

    :QUIT
    echo.
    echo Goodbye
    echo.
    echo.
    pause
    exit

    Thanks for your assistance,
    John

    Viewing 0 reply threads
    Author
    Replies
    • #1112051

      I found the issue..a line of code was incorrect.

      Incorrect statement:
      set P INPUT=Please make a selection

      Should read:
      set /P INPUT=Please make a selection

      Regards,
      John

      • #1112056

        Well done for finding the problem!

        But you might like to take account of users being perverse beyond measure. See what happens when you type 1 followed by a space in response to the question!

        It may be friendly to test just the first character of %input% as in:

        if “%input:~0,1%”==”1” goto date

        If you simply want to display the (current) values of date and time (rather than change them) note that they are held in environmental variables %date% and %time%, according to your own Regional and Language Options.

        echo %date% %time%

        BATcher

        Plethora means a lot to me.

        • #1112158

          Batcher,

          ***<>***

          I tried your suggestion and nothing really happened – the menu just refreshes. Since the 1 followed by a space is not found the code takes us back to the menu.

          You do bring up a good point and got me thinking. What happens if the typed entry is not an option? A message should be displayed indicating that the entry was not valid.

          Your thoughts would be appreciated.

          Regards,
          John
          You

          • #1112172

            Just add an ECHO with the desired message before the GOTO MENU after the last “IF” statement in your menu. You might also want to add “/P” to the DIR command, with a PAUSE on the next line, to make sure your user sees the result.

            btw, If you need to build proper menus for DOS, there are many existing well worked out solutions around, as well as command line extensions and programs that will do the job for you rather than the need to “roll your own”. If you’re doing this as a learning exercise, that’s cool

            • #1112190

              ***<>***

              I tried your suggestion and I did not get the expected results. No message was displayed.

              Regards,
              John

            • #1112196

              Tim was correct; you should end up with something like:

              if “%input%”==”” goto MENU
              if “%input%”==”1” goto TIME
              if “%input%”==”2” goto DATE
              if “%input%”==”3” goto DIR
              if /I “%input%”==”Q” goto QUIT
              ECHO You didn’t enter a valid option! Try again…
              GoTo MENU

              where the blue line is the suggested addition.

              BATcher

              Plethora means a lot to me.

            • #1112206

              My error..I had the CLS (clear screen) syntx at the beginning of the MENU. Once I remarked it out I could see the additional message.

              Thanks,
              John

    Viewing 0 reply threads
    Reply To: Batch File Assistance (XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: