Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to delete all bin and obj folders to force all projects to rebuild everything

I work with multiple projects, and I want to recursively delete all folders with the name 'bin' or 'obj' that way I am sure that all projects will rebuild everything (sometimes it's the only way to force Visual Studio to forget all about previous builds).

Is there a quick way to accomplish this (with a .bat file for example) without having to write a .NET program?

like image 555
MichaelD Avatar asked Apr 16 '09 09:04

MichaelD


2 Answers

This depends on the shell you prefer to use.

If you are using the cmd shell on Windows then the following should work:

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S bin') DO RMDIR /S /Q "%%G" FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G" 

If you are using a bash or zsh type shell (such as git bash or babun on Windows or most Linux / OS X shells) then this is a much nicer, more succinct way to do what you want:

find . -iname "bin" | xargs rm -rf find . -iname "obj" | xargs rm -rf 

and this can be reduced to one line with an OR:

find . -iname "bin" -o -iname "obj" | xargs rm -rf 

Note that if your directories of filenames contain spaces or quotes, find will send those entries as-is, which xargs may split into multiple entries. If your shell supports them, -print0 and -0 will work around this short-coming, so the above examples become:

find . -iname "bin" -print0 | xargs -0 rm -rf find . -iname "obj" -print0 | xargs -0 rm -rf 

and:

find . -iname "bin" -o -iname "obj" -print0 | xargs -0 rm -rf 

If you are using Powershell then you can use this:

Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse } 

as seen in Robert H's answer below - just make sure you give him credit for the powershell answer rather than me if you choose to up-vote anything :)

It would of course be wise to run whatever command you choose somewhere safe first to test it!

like image 118
Steve Willcock Avatar answered Sep 22 '22 13:09

Steve Willcock


I found this thread and got bingo. A little more searching turned up this power shell script:

Get-ChildItem .\ -include bin,obj -Recurse | ForEach-Object ($_) { Remove-Item $_.FullName -Force -Recurse } 

I thought I'd share, considering that I did not find the answer when I was looking here.

like image 37
Robert H. Avatar answered Sep 22 '22 13:09

Robert H.