Reputation: 15409
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?
Upvotes: 26
Views: 141103
Reputation: 2853
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
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
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
Upvotes: 1
Reputation: 130929
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.
Upvotes: 66
Reputation: 57332
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
Upvotes: 1
Reputation: 4296
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
Upvotes: 1
Reputation: 7166
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.
Upvotes: 13
Reputation: 3622
Use rmdir /S /Q modules
/Q
suppresses the confirmation prompt.
Upvotes: 4