As others have mentioned, the rd
command has the /s
switch to recursively remove sub-directories. You can combine it with the /q
switch to forcibly delete a sub-directory (and its contents) without prompting as so
rd /s /q c:\foobar
What everybody is missing is that rd
is not an exact replacement for deltree
as seemingly (almost) every page returned by Googling for windows deltree
would have you believe. The deltree
command worked for both directories and files, making it a single convenient, all-purpose deletion command. That is both of the following are valid:
deltree /y c:\foobar
deltree /y c:\baz.txt
However rd
(not surprisingly) only works for directories. As such only the first of these commands is valid while the second gives and error and leaves the file un-deleted:
rd /s /q c:\foobar
rd /s /q c:\baz.txt
Further, the del
command only works for files, not directories, so only the second command is valid while the first gives an error:
del /f /q c:\foobar
del /f /q c:\baz.txt
There is no built-in way to delete files and directories as could be done with deltree
. Using rd
and del
individually is inconvenient at best because it requires distinguishing whether a file-system object (file-/folder-name) is a file or directory which is not always possible or practical.
You can copy the deltree
command from a previous OS, however it will only work on 32-bit versions of Windows since it is a 16-bit DOS command (even in Windows 9x).
Another option is to create a batch-file that calls both del
and rd
; something like this:
::deltree.bat
@echo off
rd %* 2> nul
del %* 2> nul
You would call it as so:
deltree.bat /s /q /f c:\foobar
deltree.bat /s /q /f c:\baz.txt
This calls both rd
and del
, passing in the arguments and redirecting the output to nul
to avoid the error that one of them will invariably emit.
You will probably want to customize the behavior to accomodate or simplify parameters or allow error messages, but even so, it is not ideal and not a direct replacement for deltree
.
An alternative is to get a third-party tool, though finding one is a real exercise in search-query-crafting.
Best Answer
The command line you are using is quite dangerous (I just copied it here and reordered the switches):
Because
forfiles
returns both files and directories. Supposing there is a directory calledcontainer
that matches the search criteria (last modified 30 days ago or earlier), the executeddel
command line isdel "C:\PATH\USERS\PATH\container" /Q
, which deletes all files in there, even those not fulfilling the search criteria (note thatdel \folder
behaves likedel \folder\*
).To solve this issue, you need to filter for file or directories, like this:
I also changed the file mask from
*.*
to*
, becauseforfiles
, opposed to all internal commands, does not match files with no extension in case*.*
is given. In addition I removed the trailing\
from the path, because otherwise,forfiles
raises an error:ERROR: The directory name is invalid.
.But now for something completely different -- the answer to your actual question:
Since
forfiles /S
will return a directory before it returns its sub-items, it does not make sense do check whether the directory is empty or not in the same command line, you need to do that later.For that purpose, you can either use a second
forfiles
loop like this (also checking their last modification dates, if you like; otherwise, remove the/D -30
portion):Or alternatively, if the directory modification dates are not relevant, you can use a
for /D
loop, which is also faster: