How to create batch file that delete all the folders named `bin` or `obj` recursively?

31

19

I have the need of deleting all bin & obj folders in a folder on my PC. So, I'm thinking of a batch file to do that but I'm not famaliar with batching file in Windows. Please help.

[Edit]

After discussion with user DMA57361, I got to the current solution (still having problem though, see our comments):

Create a .bat file, and paste the below command:

start for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"

OR

start for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s "%%d"

@DMA57361: When I run your script, I get the below error. Any idea?

alt text

Nam G VU

Posted 2010-10-21T12:25:50.307

Reputation: 10 255

@Nam - why are you running for \/d \/r ... - why the double-slashs instead of for /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? – DMA57361 – 2010-10-21T13:32:27.640

@Nam this run is also not the same - in the image your command starts with for / %d in and not for /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 start and 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 start to 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 bin or obj then the command will have errors. – Nam G VU – 2010-10-22T07:30:24.363

The 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 bin or obj, 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.233

Yes, 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 @username in 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

Answers

42

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.

DMA57361

Posted 2010-10-21T12:25:50.307

Reputation: 17 581

1DO you maybe know how I can ignore one folder within this root folder where I'm running this command? – Goran Obradovic – 2012-11-23T22:39:00.290

Worked perfectly! Thx! +1 – khr055 – 2013-05-21T20:48:50.253

1

These commands should work on cmd.

del *.obj

del *.bin

arun babu

Posted 2010-10-21T12:25:50.307

Reputation: 11

0

Just do

del /s /p bin
del /s /p obj

MikeSchem

Posted 2010-10-21T12:25:50.307

Reputation: 101