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?

CodeBlue
  • 14,631
  • 33
  • 94
  • 132

7 Answers7

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • thanks, this was a very good out-of-the-box answer, works also for other commands then rmdir – Ben Croughs Apr 08 '15 at 13:33
  • 1
    useful to close a command window after a pesky PAUSE presents the "Press any key to continue..." prompt! – cody.codes Sep 01 '19 at 18:40
  • 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 text – rbansal Nov 06 '20 at 17:46
  • @rbansal , your best bet is to post a new question. Refer to this post, then ask your specific question. Good luck! – bballdave025 Jun 23 '22 at 22:52
  • This doesn't work. `echo Y | net user j: /delete` gives "No valid response was provided." – Philip Rego Oct 02 '22 at 08:07
  • @PhilipRego - Did you read the note at the bottom of the answer? I suspect the external `net` command is not reading from stdin. – dbenham Oct 25 '22 at 18:03
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.

sschrass
  • 7,014
  • 6
  • 43
  • 62
4

Use rmdir /S /Q modules

/Q suppresses the confirmation prompt.

Kevin Richardson
  • 3,592
  • 22
  • 14
3

You can do

rmdir /Q

Q is for quiet

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

Community
  • 1
  • 1
u8it
  • 3,956
  • 1
  • 20
  • 33
  • 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. – dbenham Jun 14 '16 at 13:50
  • @dbenham 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. – u8it Jun 14 '16 at 21:29
  • @dbenham 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 seperate – u8it Jun 14 '16 at 21:32
  • @dbenham 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". – u8it Jun 14 '16 at 21:37
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
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • This is extremely useful when one works in a multilingual and/or multinational coding environment/organization/life. +1 for that. – bballdave025 Jun 23 '22 at 22:50
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

pbarney
  • 2,529
  • 4
  • 35
  • 49