How do I delete directory trees via batch file on Windows 7?
Solution 1:
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.
Solution 2:
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"