Removing directory recursively

Christian K picture Christian K · Apr 9, 2013 · Viewed 13.3k times · Source

I'm doing a single Batch-File currently that deletes all contents from a specific folder on the Server. It is working so far, but directories with spaces in the Folder name will not be recognized unfortunately. I have no idea where to put the quotation marks in the script to overcome this limitation.

This is the script so far, it sits in the root of "D:\" :

cd Sharedfolder
for /f %%i in ('dir D:\Sharedfolder /B /D') do rd %%i /Q /S
del /F /S /Q *.*

It works good, but as soon as I have a Directory inside of "Sharedfolder" it will not work for that directory.

Answer

joojaa picture joojaa · Apr 9, 2013

There is a mismatch in the command use. either use for /f or use

FOR /D %%i IN (D:\Sharedfolder\*) DO rd /s /q "%%i"
del /F /S /Q *.*

Which is closer to what you were thinking. An alternative solution would be to:

rmdir /s /q D:\Sharedfolder
mkdir D:\Sharedfolder

Tough this may have some problems elsewhere in the system.