Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Making [Y/N] questions in batch files  (Read 218713 times)

0 Members and 1 Guest are viewing this topic.

PirateSamir

    Topic Starter


    Rookie

    Making [Y/N] questions in batch files
    « on: January 23, 2009, 02:48:47 PM »
    How would i program it so that the only two possible answers are Y and N?

    I know i have to use the if command for the second part, but how do i make it so that the only pressable buttons are Y and N or if i cant do that how do i make it so that if i press Y a certain thing happens, and if i press N a certain thing happens and if i press any other button then it will repeat the question.

    Thnx in advance.
    Quantum bonus pro quantum numerus.

    hiteshsavla



      Rookie

      Thanked: 1
      Re: Making [Y/N] questions in batch files
      « Reply #1 on: January 23, 2009, 03:16:25 PM »
      Code: [Select]
      @ECHO OFF

      :choice
      set /P c=Are you sure you want to continue[Y/N]?
      if /I "%c%" EQU "Y" goto :somewhere
      if /I "%c%" EQU "N" goto :somewhere_else
      goto :choice


      :somewhere

      echo "I am here because you typed Y"
      pause
      exit

      :somewhere_else

      echo "I am here because you typed N"
      pause
      exit

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: Making [Y/N] questions in batch files
      « Reply #2 on: January 23, 2009, 03:21:18 PM »
      Code: [Select]
      @ECHO OFF

      :choice
      set /P c=Are you sure you want to continue[Y/N]?
      if /I "%c%" EQU "Y" goto :somewhere
      if /I "%c%" EQU "N" goto :somewhere_else
      goto :choice


      :somewhere

      echo "I am here because you typed Y"
      pause
      exit

      :somewhere_else

      echo "I am here because you typed N"
      pause
      exit

      That code should work. Just remember to use the /I switch so it isn't case sensitive.
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      PirateSamir

        Topic Starter


        Rookie

        Re: Making [Y/N] questions in batch files
        « Reply #3 on: January 23, 2009, 03:30:06 PM »
        unfortunately that doesnt work. It doesn't matter what you press, it will go to the Y option every single time. :/
        also could you explain to me the /P and the /I before choice.

        Btw, im using Win XP and im not sure but i dont think that the choice command works.
        Quantum bonus pro quantum numerus.

        hiteshsavla



          Rookie

          Thanked: 1
          Re: Making [Y/N] questions in batch files
          « Reply #4 on: January 23, 2009, 03:35:12 PM »
          Try again.. Copy the code again to a new file and try again.
          I am surprised it doesn't work for you. Cause its a fairly straight forward code. It should work!!

          As for your question:

          /I ---- makes the character Case insensitive
          /P ---- Prompt the user with the question that follows and set the answer to the variable.

          PirateSamir

            Topic Starter


            Rookie

            Re: Making [Y/N] questions in batch files
            « Reply #5 on: January 23, 2009, 03:52:36 PM »
            ok it works this time, lol weird.

            One more question. is there a way to represent all the characters? If so, can you do something like exclude certain characters while keeping the rest?
            To make it more clear ill give you an example of what i want to do. I want a batch file that will ask you yes or no questions and the only buttons that will wield you a result would be Y and N, while the rest of the characters would just repeat the question. Is there a quicker way to program the rest of the characters to repeat your question?
            Hope its clear to you what im saying.
            Quantum bonus pro quantum numerus.

            hiteshsavla



              Rookie

              Thanked: 1
              Re: Making [Y/N] questions in batch files
              « Reply #6 on: January 23, 2009, 03:56:05 PM »
              Honestly not too sure what you mean.. Could you post an example.

              The code in my previous post will only work if you type Y/N otherwise it will ask you the same question again and again.

              PirateSamir

                Topic Starter


                Rookie

                Re: Making [Y/N] questions in batch files
                « Reply #7 on: January 23, 2009, 03:59:22 PM »
                OMG thank you! :D
                This is exactly what i wanted :D
                Quantum bonus pro quantum numerus.

                macdad-



                  Expert

                  Thanked: 40
                  Re: Making [Y/N] questions in batch files
                  « Reply #8 on: January 23, 2009, 04:10:05 PM »
                  you could try CHOICE.EXE, the NT version of it. it has more features than Set /P
                  such as it automatically chooses for you if you dont specify what choice you want after so many seconds.
                  If you dont know DOS, you dont know Windows...

                  Thats why Bill Gates created the Windows NT Family.

                  Dias de verano

                  • Guest
                  Re: Making [Y/N] questions in batch files
                  « Reply #9 on: January 24, 2009, 01:12:33 AM »
                  you could try CHOICE.EXE, the NT version of it. it has more features than Set /P
                  such as it automatically chooses for you if you dont specify what choice you want after so many seconds.

                  THis can be hard to find.

                  Open source choice.exe here:

                  http://winsupport.org/packages/choice.exe

                  BatchFileCommand



                    Hopeful
                  • Thanked: 1
                    Re: Making [Y/N] questions in batch files
                    « Reply #10 on: January 24, 2009, 07:13:35 AM »
                    As a code you can also use.

                    Code: [Select]
                    if /i "%variable%"=="string"
                    You can also put == instead of equ .
                    οτη άβγαλτος μεταφ βαθμολογία

                    PirateSamir

                      Topic Starter


                      Rookie

                      Re: Making [Y/N] questions in batch files
                      « Reply #11 on: January 24, 2009, 08:58:35 AM »
                      thanks guys, you really helped me.
                      Quantum bonus pro quantum numerus.

                      macdad-



                        Expert

                        Thanked: 40
                        Re: Making [Y/N] questions in batch files
                        « Reply #12 on: January 24, 2009, 11:19:57 AM »
                        Anytime  ;)
                        If you dont know DOS, you dont know Windows...

                        Thats why Bill Gates created the Windows NT Family.

                        BatchFileCommand



                          Hopeful
                        • Thanked: 1
                          Re: Making [Y/N] questions in batch files
                          « Reply #13 on: January 25, 2009, 02:17:53 PM »
                          While choice has more features then set /p. It is also more of a hassle. set /p is simple and easy. But yes, choice does have things such as waiting for a certain amount of time before giving a default answer.
                          οτη άβγαλτος μεταφ βαθμολογία

                          WillyW



                            Specialist
                          • Thanked: 29
                          • Experience: Experienced
                          • OS: Windows XP
                          .



                          macdad-



                            Expert

                            Thanked: 40
                            Re: Making [Y/N] questions in batch files
                            « Reply #15 on: January 25, 2009, 06:22:57 PM »
                            the NT version is the one that you would want.
                            If you dont know DOS, you dont know Windows...

                            Thats why Bill Gates created the Windows NT Family.

                            PirateSamir

                              Topic Starter


                              Rookie

                              Re: Making [Y/N] questions in batch files
                              « Reply #16 on: January 26, 2009, 10:57:24 AM »
                              3.1 or 3.5?
                              Quantum bonus pro quantum numerus.

                              Dias de verano

                              • Guest
                              Re: Making [Y/N] questions in batch files
                              « Reply #17 on: January 26, 2009, 11:23:47 AM »
                              3.1 or 3.5?

                              Either, or NT4, or Win2K resource kit

                              PirateSamir

                                Topic Starter


                                Rookie

                                Re: Making [Y/N] questions in batch files
                                « Reply #18 on: January 26, 2009, 11:38:43 AM »
                                k thnx, i got 3.5
                                gonna learn to use it now :D
                                Quantum bonus pro quantum numerus.

                                Dias de verano

                                • Guest
                                Re: Making [Y/N] questions in batch files
                                « Reply #19 on: January 26, 2009, 11:43:35 AM »
                                k thnx, i got 3.5
                                gonna learn to use it now :D
                                Code: [Select]
                                Microsoft Windows XP [Version 5.1.2600]
                                (C) Copyright 1985-2001 Microsoft Corp.

                                C:\>choice /?
                                CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]

                                /C[:]choices Specifies allowable keys. Default is YN
                                /N           Do not display choices and ? at end of prompt string.
                                /S           Treat choice keys as case sensitive.
                                /T[:]c,nn    Default choice to c after nn seconds
                                text         Prompt string to display

                                ERRORLEVEL is set to offset of key user presses in choices.

                                PirateSamir

                                  Topic Starter


                                  Rookie

                                  Re: Making [Y/N] questions in batch files
                                  « Reply #20 on: January 26, 2009, 11:55:00 AM »
                                  ok, 2 questions
                                  how would define your default choice?
                                  and
                                  what is errorlevel? (i've seen it quite oftenly in batch files)  ???
                                  Quantum bonus pro quantum numerus.

                                  Dias de verano

                                  • Guest
                                  Re: Making [Y/N] questions in batch files
                                  « Reply #21 on: January 26, 2009, 12:44:09 PM »
                                  ok, 2 questions
                                  how would define your default choice?
                                  and
                                  what is errorlevel? (i've seen it quite oftenly in batch files)  ???


                                  Example

                                  Default choice is Y, timeout is 10 seconds

                                  Code: [Select]
                                  choice /C:YN /T:Y,10 Press Y or N
                                  echo errorlevel is: %errorlevel%

                                  If Y was pressed (or timeout occurred) the %errorlevel%  will be 1 (because Y is choice 1) and if N was pressed %errorlevel% will be 2 (because N is the second choice)

                                  errorlevel is a system variable which is set by a program. Often 0=no error, 1 or more means an error. Choice uses it to pass the key pressed.

                                  Code: [Select]
                                  S:\>chtest1.bat
                                  Press Y or N [Y,N]?Y
                                  errorlevel is: 1

                                  S:\>chtest1.bat
                                  Press Y or N [Y,N]?N
                                  errorlevel is: 2

                                  PirateSamir

                                    Topic Starter


                                    Rookie

                                    Re: Making [Y/N] questions in batch files
                                    « Reply #22 on: January 26, 2009, 12:49:58 PM »
                                    so can errorlevel be used for anything else?
                                    Quantum bonus pro quantum numerus.

                                    Dias de verano

                                    • Guest
                                    Re: Making [Y/N] questions in batch files
                                    « Reply #23 on: January 26, 2009, 12:59:32 PM »
                                    so can errorlevel be used for anything else?

                                    Yes.

                                    Code: [Select]
                                    dir *.txt
                                    if %errorlevel% GTR 0 echo "No text files"

                                    Plenty of information if you use Google.


                                    PirateSamir

                                      Topic Starter


                                      Rookie

                                      Re: Making [Y/N] questions in batch files
                                      « Reply #24 on: January 26, 2009, 01:03:35 PM »
                                      ok thnx :)
                                      Quantum bonus pro quantum numerus.

                                      ht

                                      • Guest
                                      Re: Making [Y/N] questions in batch files
                                      « Reply #25 on: January 08, 2011, 03:20:54 PM »
                                      Code: [Select]
                                      @ECHO OFF

                                      :choice
                                      set /P c=Are you sure you want to continue[Y/N]?
                                      if /I "%c%" EQU "Y" goto :somewhere
                                      if /I "%c%" EQU "N" goto :somewhere_else
                                      goto :choice


                                      :somewhere

                                      echo "I am here because you typed Y"
                                      pause
                                      exit

                                      :somewhere_else

                                      echo "I am here because you typed N"
                                      pause
                                      exit


                                      Thanks hiteshsavla - Exactly what I required, saved me a lot of time.  (I write batch files so infrequently these days that it takes time to get back into it)