Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
@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 Oct 21 '10 at 13:32
    
@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 Oct 21 '10 at 13:47
    
@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 Oct 21 '10 at 13:53
    
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 Oct 21 '10 at 14:06
    
Actually, try prefixing the batch command with start and see what happens. Not ideal, but I wonder if that may help. –  DMA57361 Oct 21 '10 at 14:08

1 Answer 1

up vote 21 down vote accepted

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. Althought 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 recieve an error message for each file encounted 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 otherwords they can be ignored safely.

share|improve this answer
1  
DO you maybe know how I can ignore one folder within this root folder where I'm running this command? –  Goran Obradovic Nov 23 '12 at 22:39
    
Worked perfectly! Thx! +1 –  Mike May 21 '13 at 20:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.