Removing directory recursively

13,311

Solution 1

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.

Solution 2

This is the complete answer you are looking for, Try this one:

for /f "delims=" %%x in ('dir /b /ad abc*') do rd /s /q "%%x"

I have tried, It works.

Share:
13,311
Christian K
Author by

Christian K

Updated on June 04, 2022

Comments

  • Christian K
    Christian K almost 2 years

    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.

  • Christian K
    Christian K about 11 years
    No it does unfortunately not work. It tries to delete "D:\Sharedfolder\D:\Sharedfolder" and its subdirectories. Also, i had to place the "%%I" before the /S /Q to make it do anything at all.
  • AB Bolim
    AB Bolim about 11 years
    I have edited the answer, and also checked the solution. Its work fine. All the best.