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.
@Nam - why are you running
for \/d \/r ...- why the double-slashs instead offor /d /r ...? And running the "full" version with/qat 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? – DMA57361 – 2010-10-21T13:32:27.640@Nam this run is also not the same - in the image your command starts with
for / %d inand notfor /d /r . %d in- you have to use the entire command below, each bit has a different function and is important. – DMA57361 – 2010-10-21T13:47:23.950@Nam Opps, my apologies, you are entirely right, it doesn't print the first few switches! However, it doesn't throw that error and seems to work correct, for me. Have you tried simply running the command from the command line, instead of trying to run it from within a batch file? – DMA57361 – 2010-10-21T13:53:12.440
Hmm, how strange. Possibly your Win7 is doing something different with the batch compared to my WinXP then, not sure what. I'll be able to check on Win7 at home this evening, otherwise I'm afraid I'm stuck at the moment... – DMA57361 – 2010-10-21T14:06:38.250
Actually, try prefixing the batch command with
startand see what happens. Not ideal, but I wonder if that may help. – DMA57361 – 2010-10-21T14:08:27.717@DMA57361: I works now when we add
startto the beginning. Only problem remains: if a file with the same names exists, the command doesn't work. – Nam G VU – 2010-10-22T02:59:09.523@Nam Sorry, a file with the same name as what? – DMA57361 – 2010-10-22T07:25:10.397
@DMA57361: Firstly, please remove your old comments since they are out-dated already. About same name files, they are named
binorobjthen the command will have errors. – Nam G VU – 2010-10-22T07:30:24.363The comments may appear outdated, but the full edit history of every post is public, so removing my comments will remove context from those edits - and anyone reading this in the future (because they had the same question) might have the same problems as you've had, so keeping these comments here will help them solve those issue. In response: I've just tested and, yes I get errors if there are files named
binorobj, but it still deleted everything it's supposed to (and nothing else), are you getting the same? If so, I don't see that as a problem. And I don't know an easy fix. – DMA57361 – 2010-10-22T07:39:52.233Yes, I have the same problem DMA. Just don't like the error occurs :). About the comments, it's all up to you my friend! Nice discussing with you! – Nam G VU – 2010-10-22T11:21:12.637
Please update your answer with full details that we have discussed. I'm ready to select yours as accepted. – Nam G VU – 2010-10-22T11:22:03.133
Hm, I didn't get any errors while running. – Sathyajith Bhat – 2010-10-22T19:28:36.917
@Nam answer updated as requested. Note that if you use
@usernamein your comments the other person recieves a notification - it makes it easier for them to remember to come back and continue answering, especially useful when a thread like this lasts a while! – DMA57361 – 2010-10-26T10:25:12.690