This has been previously answered over on Stack Overflow, which is where I've taken the main thrust of this answer from.
Try the following command, you can run it from within cmd
:
for /d /r . %d in (bin,obj) do @if exist "%d" rd /s/q "%d"
If you need other folders to be changed, then just add new items to the (bin,obj)
set in the middle of the command.
This will delete everything matched without warning and without using the recycle bin - so, if you want a bit of extra safety, drop the /q
from the call to rd
at the end, and the system should ask you before each deletion.
for /d /r . %d in (bin,obj) do @if exist "%d" rd /s "%d"
If you intend to run the command from within a batch file, you will need to replace every instance of the variable %d
with %%d
, like so:
for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"
OR
for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s "%%d"
As per the conversion had in the question comments
If the command refuses to run in a batch file (unable to replicate here), try prefixing the command with start
. Although this will start the process to run concurrently with the batch file, which may cause other issues, it seems more likely to work correctly.
Also, if you have files named obj
or bin
within the folder tree the command is working on, then you will receive an error message for each file encountered that has a matching name. These matched files are not deleted, and should not get in the way of the command deleting what it should. In other words, they can be ignored safely.
for \/d \/r ...
- why the double-slashs instead offor /d /r ...
? And running the "full" version with/q
at the end - as I state - deletes everything without warning so returns no output (ie, it doesn't tell you when it works, only when it has an error), are you sure the second run above didn't work correctly?for / %d in
and notfor /d /r . %d in
- you have to use the entire command below, each bit has a different function and is important.start
and see what happens. Not ideal, but I wonder if that may help.