Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Yes/No Batch file (XP SP2)

    I've searched, to no avail, for a batch file that will execute certain instructions, or not, depending on the user input. If the user inputs "y" OR "Y", the instructions will be executed. Anything other than "y" OR "Y" will exit the batch file. Since some of our users think that monitors are mind readers, I would like to do without the use of the "ENTER" key after the user inputs the instruction. Any guidance will be appreciated. Thanks!!!

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Yes/No Batch file (XP SP2)

    There is quite a good discussion of this topic here.

    Probably the easiest method is to use SET /P, something like this
    <code>
    @echo off
    SET /P YNvar=Enter Y or N:
    if "%YNVar%"=="Y" goto doYES
    if "%YNVar%"=="y" goto doYES
    goto doNO
    :doYES
    echo The user entered Y
    goto done
    :doNO
    echo The user didn't enter Y
    :done
    pause
    </code>
    StuartR

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

    Re: Yes/No Batch file (XP SP2)

    Here's a fragment that does vaguely what you want (obviously you will need labels :answer_yes and :answer_no to get it to work properly):
    <code>
    @echo off
    :ask
    set answer=
    set /p answer="Is today Christmas Eve? [y/n] "
    if "%answer%"=="" goto ask
    if /i "%answer:~0,1%"=="y" echo Answer was %answer% & goto answer_yes
    echo Answer was %answer% & goto answer_no</code>


    This accepts<UL><LI>just the Enter key, whereupon it repeats the question<LI>any number of characters starting with y or Y (such as "Yes, stupid") and followed by Enter as "Y"<LI>anything else followed by Enter as No[/list]. If you want to accept one and only one character without pressing Enter you will need to have a tiny utility program to do that. If I remember correctly, MS-DOS 6 had a utility called CHOICE.EXE or CHOICE.COM which might fit the bill.
    BATcher

    Time prevents everything happening all at once...

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Yes/No Batch file (XP SP2)

    Thanks to BATcher and StuartR!!!

Posting Permissions

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