34

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

13
  • @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, 2010 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, 2010 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, 2010 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, 2010 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, 2010 at 14:08

3 Answers 3

46

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.

2
  • 1
    DO you maybe know how I can ignore one folder within this root folder where I'm running this command? Nov 23, 2012 at 22:39
  • Worked perfectly! Thx! +1
    – khr055
    May 21, 2013 at 20:48
1

These commands should work on cmd.

del *.obj

del *.bin

1
  • This deletes files not folders.
    – Missy
    Nov 30, 2023 at 15:57
0

Just do

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .