Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to supply console input ( yes / no ) as part of batch file on Windows.

Tags:

I am writing a simple batch file (remove.bat) to remove a directory and all its subdirectories. The file contains the following command-

rmdir /S modules 

where modules is the name of the non-empty directory.

I get the following message -

C:\...\bin>rmdir /S modules modules, Are you sure (Y/N)? 

How can I supply through the batch file the console input "Y" to the Y/N question above? Is there a command that can do this?

like image 899
CodeBlue Avatar asked Feb 28 '13 23:02

CodeBlue


People also ask

How do I use YES in cmd?

Used without any command line parameters, the yes command behaves as though you were typing “y” and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.

Can a batch file ask for input?

You are able to prompt a user for input using a Batch script function.

How do you automate input in command prompt?

To avoid this manual input, use the command "echo Y | del /P " in your batch script to answer the prompt. You can try this echo command to pass input (ex: answer for username and password prompts) to your console application, when it is invoked through batch script.

What is %% A in batch script?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

As others have pointed out, you should use the /Q option. But there is another "old school" way to do it that was used back in the day when commands did not have options to suppress confirmation messages. Simply ECHO the needed response and pipe the value into the command.

echo y|rmdir /s modules 

I recommend using the /Q option instead, but the pipe technique might be important if you ever run into a command that does not provide an option to suppress confirmation messages.

Note - This technique only works if the command reads the input from stdin, as is always the case with cmd.exe internal commands. But this may not be true for some external commands.

like image 80
dbenham Avatar answered Sep 19 '22 15:09

dbenham