Results 1 to 8 of 8
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Batch File Assistance (XP)

    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

    ATE
    DATE
    goto MENU

    IR
    DIR
    goto MENU

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



    Thanks for your assistance,
    John

  2. #2
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File Assistance (XP)

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

    Incorrect statement:
    set P INPUT=Please make a selection

    Should read:
    set <font color=red>/P</font color=red> INPUT=Please make a selection


    Regards,
    John

  3. #3
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts

    Re: Batch File Assistance (XP)

    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

    Time prevents everything happening all at once...

  4. #4
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File Assistance (XP)

    Batcher,

    ***<<See what happens when you type 1 followed by a space in response to the question!>>***

    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

  5. #5
    5 Star Lounger
    Join Date
    Dec 2002
    Location
    Perth, Western Australia, Australia
    Posts
    730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File Assistance (XP)

    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 <img src=/S/cool.gif border=0 alt=cool width=15 height=15>
    <font face="Comic Sans MS" color="blue">TimOz</font>
    <img src=/S/flags/Finland.gif border=0 alt=Finland width=30 height=18> <img src=/S/flags/Australia.gif border=0 alt=Australia width=30 height=18>

  6. #6
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File Assistance (XP)

    ***<<Just add an ECHO with the desired message before the GOTO MENU after the last "IF" statement in your menu>>***

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

    Regards,
    John

  7. #7
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts

    Re: Batch File Assistance (XP)

    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
    <font color=blue>ECHO You didn't enter a valid option! Try again...</font color=blue>
    GoTo MENU

    where the blue line is the suggested addition.
    BATcher

    Time prevents everything happening all at once...

  8. #8
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File Assistance (XP)

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •