41

I want to delete the whole content of a specified folder on Windows 7 via batch file. My problem is, that 'del' or 'erase' only deletes the files, not the folders and 'rmdir' or 'rd' always deletes the specified folder with its content, but I only want to delete the content, not the folder itself. I tried the command 'rmdir /S /Q "C:\Share\*"' which gave me a syntax error.

What is the correct way to do this?

I am working on Windows 7 Professional 64-bit and have admin permissions.

7 Answers 7

41

Your batch file will need to run two commands, one to clear out the files then one to remove the child directories. I've assumed the directory you want to remove is C:\Share\

The batch file should look something like this:

del /s /f /q c:\share\*.*
for /f %%f in ('dir /ad /b c:\share\') do rd /s /q c:\share\%%f

del /s /f /q will recursively search through the directory tree deleting any files (even read only files) without prompting for confirmation.

The second line loops through all the sub directories (which should now be empty) and removes them.

Short of deleting the entire folder and recreating it (which I don't think you want to do due to permissions?) this should be the easiest way to clean the folder out.

6
  • If I do a little correction here "for /f %%f" -> "for /f %f" then it works fine :) Thank you very much.
    – Michael K
    Aug 26, 2011 at 13:03
  • 7
    del is not necessary. rd/s/q will delete both files and subfolders.
    – user1686
    Aug 26, 2011 at 13:51
  • 1
    @grawity the del is mainly there for any files in the root folder, could remove the recursion switch though.
    – Windos
    Aug 26, 2011 at 19:34
  • 5
    @MichaelK, %%f is for use in batch files, %f for when it's a single command straight to the command line.
    – Windos
    Aug 26, 2011 at 19:36
  • 1
    If any of the subdirectory names contain spaces, you can add "delims=" to process them correctly (for /f "delims=" %%f in ...): See stackoverflow.com/q/5553040/64918
    – goldPseudo
    May 7, 2015 at 19:19
30
rmdir /s/q C:\Share

You get a "Syntax error" because rmdir only accepts complete names, not wildcards. (In cmd.exe, wildcard expansion is left to the individual programs; not all of them do.)

If you have many directories starting with Share..., use a for loop.

for /d %f in (C:\Share*) do rmdir /s/q "%f"
1
  • 3
    I do not want to delete the share folder.
    – Michael K
    Aug 26, 2011 at 13:02
13

Try this in a command prompt:

rd /s/q "C:\Share"
1
  • 3
    This removed parent folder as well, not content of the folder. Sep 18, 2016 at 19:52
4

What about ?

rmdir /S /Q "target"
mkdir "target

EDIT: of course this solution applicable only when you can tolerate a momentary folder absence.

2
  • This would delete the original folder.
    – Michael K
    Aug 7, 2015 at 12:14
  • @MichaelK My task was able to tolerate a momentary folder absence, so I went with this solution. I thought, that it is simpler and more readable than for /f in cases similar to mine. Just wanted to share Aug 7, 2015 at 12:27
2
for /f "delims=" %%f in ('dir /ad /b c:\share\') do rd /s /q c:\share\%%f

This does not work if the subdirectories contain other directories that contain spaces.

In order to make this work, I needed to quote the final string, like this

for /f "delims=" %%f in ('dir /ad /b c:\share\') do rd /s /q "c:\share\%%f"

Apparently, this causes the command to work on the quoted string instead of just the string itself.

1

simply:

rmdir /s /q "path"

mkdir "path"

1
0

I would try this in the folder where all subfolders should be deleted but the root (and files in the root) left as they are: for /D %v in (*) do rd /s/q %v

for /D matches directories and rd/s/q deletes each at a time

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.