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

7

25

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?

Everick answered 28/2, 2013 at 23:47 Comment(0)
L
65

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.

Lorislorita answered 1/3, 2013 at 0:17 Comment(6)
thanks, this was a very good out-of-the-box answer, works also for other commands then rmdirPithecanthropus
useful to close a command window after a pesky PAUSE presents the "Press any key to continue..." prompt!Apfelstadt
I am trying to generate the ssh keys silently on windows. This approach does not seem to work. echo ''| ssh-keygen.exe -t rsa -f ./id_rsa -q. I don't want any password for the key. so I am keeping it empty. But the prompt is not picking up the echo textOctangular
@Octangular , your best bet is to post a new question. Refer to this post, then ask your specific question. Good luck!Denunciation
This doesn't work. echo Y | net user j: /delete gives "No valid response was provided."Doyon
@PhilipRego - Did you read the note at the bottom of the answer? I suspect the external net command is not reading from stdin.Lorislorita
T
12

Do rmdir /S for deleting a non-empty directory and do rmdir /Q for not prompting. Combine to rmdir /S /Q for quietly delete non-empty directories.

Thayer answered 28/2, 2013 at 23:52 Comment(0)
B
4

Use rmdir /S /Q modules

/Q suppresses the confirmation prompt.

Barbican answered 28/2, 2013 at 23:50 Comment(0)
S
3

You can do

rmdir /Q

Q is for quiet

Somatoplasm answered 28/2, 2013 at 23:49 Comment(0)
S
1

I just want to add that, although not applicable to Rmdir, a force switch may also be the solution in some cases. So in a general sense you should look at your command switches for /f, /q, or some variant thereof (for example, Netdom RenameComputer uses /Force, not /f).

The echo pipe is a neat trick and very useful to keep around since you wont always find an appropriate switch. For instance, I think it's the only way to bypass this Y/N prompt...

Echo y|NETDOM COMPUTERNAME WorkComp /Add:Work-Comp

Link to nearly identical StackOverflow post

Saturation answered 11/8, 2015 at 19:41 Comment(4)
Two years later, and you decide to post an answer that does not provide any new information!? Why? And your first sentence is wrong - the /Q option is the "force" option, as was already stated in the three prior answers.Lorislorita
@Lorislorita I see your point, but... 1.) Why is two years later a problem when this is still a result on Google? 2.) Example of "new information" is that you may have to hunt for a similar switch for another command, it won't always be /q or /f, but those are good first guesses. 3.) I guess you could nit pick over the first sentence, but it brings up a good point that other posts here don't mention. The word "force" makes me expect "Y" behavior, while the word "quiet" makes me think "Y" or "N". If /S isn't provided, RmDir /Q actually provides the "N" behavior for a non-empty folder.Saturation
@Lorislorita I agree this answer doesn't add a lot though... I really posted it as an answer to my own question concerning Netdom. I wanted to share what I found worked in different cases for that, but thought it was too similar to be a separate question, and I always hesitate to post and answer my own questions... so I kind of appended it here... maybe it should be seperateSaturation
@Lorislorita Also, this answer included a link to a similar post, where it can be seen that quiet <> force. They are obviously different when considering the case "del /f /q".Saturation
P
1

If you are not dealing with a windows with a english/us locale you might need to retrieve the answers needed for your machine:

@echo off

setlocal

set "ans_yes="
set "ans_no="
set "ans_all="

copy /y nul # >nul

for /f "tokens=2-7 delims=[(/)]" %%a in ( '
    copy /-y nul # ^<nul
' ) do if not defined ans_yes if "%%~e" == "" (
    set "ans_yes=%%~a"
    set "ans_no=%%~b"
    set "ans_all=%%~c"
) else (
    set "ans_yes=%%~a"
    set "ans_no=%%~c"
    set "ans_all=%%~e"
)

del /q #

set "ans_yes=%ans_yes: =%"
set "ans_no=%ans_no: =%"
set "ans_all=%ans_all: =%"

set "ans_y=%ans_yes:~0,1%"
set "ans_n=%ans_no:~0,1%"
set "ans_a=%ans_all:~0,1%"

endlocal & (
    set "ans_y=%ans_y%"
    set "ans_n=%ans_n%"
    set "ans_a=%ans_a%"
)

echo %ans_y%|rmdir /s modules
Plexiglas answered 9/11, 2020 at 17:56 Comment(1)
This is extremely useful when one works in a multilingual and/or multinational coding environment/organization/life. +1 for that.Denunciation
G
1

YES.EXE

Yes is a fantastic tool that will continually answer Yes, No or whatever to any process that is asking for input.

If you run it by itself, it just outputs y + enter over and over. But that's not really what it's meant for. It's meant for piping into another program that is looking for a response to a prompt.

The simplest use case:

yes|rd temp /s

You can use yes.exe to output any argument or string: (stupid example warning):

yes hello world for a simple basic 10 PRINT "Hello World": GOTO 10

What it's really for:

It's meant for command line tools that can have a repetitive prompt but don't have a built-in /y or /n.

For example, you're copying files and keep getting the Overwrite? (Yes/No/All) prompt, you get stuck having to hammer to "N" key for No. Here's the fix:

yes n|copy * c:\stuff

How to get it?

This is just a small part of the GNU Core Utils for Windows, which provides the basic Linux commands to Windows people. VERY, VERY useful stuff if you write a lot of batch files.

If you have Git for Windows, you already have it, along with the rest of the GNU Core Utils. Check your PATH for it. It's probably in C:\Users\USERNAME\AppData\Local\Programs\Git\usr\bin

If you need to download the Windows binaries, they're available from a lot of different places, but the most popular is probably at https://cygwin.com/packages/summary/coreutils.html

Grume answered 6/8, 2021 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.