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