I <3 Steve McConnell*
Coding Horror
programming and human factors
by Jeff Atwood

August 21, 2005

Clean Sources Plus

Omar Shahine's Clean Sources is a nifty little right-click app for .NET developers:

This application does one thing. It adds an explorer shell menu to folders that when selected will recursively delete the contents of the bin, obj and setup folders. If you have a .NET project that you wish to share with someone, this is useful to remove the unnecessary stuff from the folder before you zip it up and send it off.

There's one glaring omission here, though. The source control bindings aren't removed! And neither are the local user setting files. I finally had some time, so...

Presenting Clean Sources Plus. It adds a right-click menu to folders that does the following:

  • Removes bin, obj, Debug and Release folders
  • Removes source control bindings from project and solution files
  • Removes user setting files

The result is a very clean, minimal set of .NET solution files, suitable for upload or sharing.

Updated to version 1.1 on 11/10/05 with the following new features:

  • Added second context menu "Clean and Zip Sources" which also zips the entire folder contents into a single zip file. This zip file is placed in the root folder and shares the same name as the folder.
  • The regex patterns used to determine what files and folders to delete are now set in the .config file. This way you can customize what gets deleted without recompiling.

I tested this with both C# and VB projects, but I'm not 100% sure it works for all other types of projects. It shouldn't break anything, but I may have missed some oddball project type (database? setup?) source bindings. And I only tested against the typical SourceSafe bindings. Anyway, test it out and let me know if there are any issues.*

* If this app deletes all your source code, then it's Omar's fault.

Posted by Jeff Atwood    View blog reactions

 

« Despite the incredible slowness and the sparseness of features, this is really really cool Is DoEvents Evil, Revisited »

 

Comments

LOL!

Omar Shahine on August 21, 2005 10:26 PM

> is useful to remove the unnecessary stuff from the folder before you zip it up and send it off.

Now that I'm re-reading this, it seems logical that there should be an additional right click to do this as well.. why not cut out the middleman? eg

"Clean Sources"
"Clean Sources and Zip"
"Clean Sources, Zip and Email"

But what's the best way to ZIP the files without adding a bunch of dependencies to our solution? We could use the zip assembly.. or is there an easy-ish way to programmatically drive the "compressed folders" support in XP?

Jeff Atwood on August 21, 2005 10:37 PM

Why not just skip the clean step and do "Zip Sources"? It can just omit all the files that would have been cleaned but doesn't touch your disk, except to write the new Zip file.

David Avraamides on August 22, 2005 07:35 AM

Very cool utility, Jeff.

One thing on the Zipping (which is a great idea)... there are still W2K users our here (yeah, yeah, I know), so please don't bind this utility to XP+, if possible.

David's got a great point... A "Safe Zip Sources" option, in addition to the others, that didn't touch original files would be pretty cool too.

The hard part would be the source control binding removal... Maybe backup/rename existing file, remove bindings and save as original file name, zip, copy old/original file over the cleaned version, delete backup.. etc, etc...

LOL, easy for me to say... Since I have the source maybe I should shut-up and just do it!!! ;)

Damn open source... LOL

Greg on August 22, 2005 10:55 AM

This sounds handy, but I wish VS.NET would have let me simply organize my sources the way I want in the first place. I prefer having all source separate from bin and obj directories, so that it is easier to perform these sorts of clerical tasks.

Ned Batchelder on August 22, 2005 02:46 PM

Great utility! Thanks for enhancing Omar's already slick tool, Clean Sources. This works great!

Brian Swiger on August 22, 2005 03:43 PM

Very nice! Removing source bindings by hand is pain, I've had to do it on a few occasions. Thanks!

SharpZipLib supports in memory compression, its in the FAQ. It's a very handy feature. http://wiki.sharpdevelop.net/default.aspx/SharpZipLib.FrequentlyAskedQuestions

Daniel F on August 22, 2005 09:16 PM

Gah, slapped that submit button a wee bit early. As I was about to say...
Should be relatively easy to edit the files in memory, zip them in memory, then save the zip somewhere for emailing. Not sure what the free options are for emailing without persisting the zip to disk, I've only ever used commercial email components. I probably should download the source and see how it's currently done, eh? :-)

Thanks again Jeff!

Daniel F on August 22, 2005 09:21 PM

Nice, thanks.

One possible (and easy) improvement:
Can you also try to delete _svn folders? They are created by a special version of Subversion compatible with VS.NET web projects.

Thanks.
Tomas

Tomas Holub on August 29, 2005 08:40 AM

Thanks Jeff. Just what I was looking for.

I made a small change to store the Directory, File and Binding pattern in a .config file so it's easy to change when you find new directories. I use Vault which has an _sgbak folder to remove.

Works great!

Richard

Richard Edwards on September 6, 2005 09:48 AM

I updated the project to version 1.1 incorporating some of the features in the comments here. The details are in the post, but to summarize:

- "Clean and Zip Sources" menu item
- file and folder deletion regex patterns are set in .config file

I couldn't figure out a way to (easily) automatically spawn an email with an attachment. Using a mailto: URL with an attachment param definitely doesn't work..

I didn't implement the "safe" no-touch zip option yet. Maybe for version 1.2.

I'd also like the context menu to be a submenu, since we may have 3-4 options in it once the "safe" options are online. That's pushing it a bit for a right-click menu on every folder. But I couldn't figure out how to set up the registry to achieve that. Hints??

Jeff Atwood on November 10, 2005 05:35 PM

Perfect! You read my mind with the app.config regex setup. Good show!

Jon Galloway on December 16, 2005 03:29 PM

Thanks Jeff, v. useful.

I quickly knocked up a MAPI class, so you can now create the emails. You can find it here.

<a href="http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=71&PostID=1">http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=71&PostID=1</a>;

thanks
andrew

Andrew Baker on March 10, 2006 05:29 PM

Great tool! I've done a similar tool that finds orphaned items, i.e. items that are on disk but not specified as part of a project. The link is on my blog. I can't post the url of my blog for some reason. Anyway, google for "steve dunn" blog. Cheers, Steve Dunn.

Steve Dunn on October 22, 2006 02:01 AM

Hi Jeff, thanks for the useful utility. While trying to set the configuration file to be able to delete .svn directories, I've incurred into the problem that the Directory.Delete method fails if the directory contains readonly files, so I've slightly edited your code to first navigate all the directory subtree and remove all the readonly attributes from the files. Your DeletePath method becomes:

Private Sub DeletePath(ByVal path As String)
Try
If IsDirectory(path) Then
SetAllFilesReadWrite(path) ' added this call
Directory.Delete(path, True)
Else
If GetFileReadOnly(path) Then
SetFileReadOnly(path, False)
End If
File.Delete(path)
End If
Catch ex As Exception
DumpException(ex, "delete file or path", path)
End Try
End Sub

And the body of the SetAllFilesReadWrite method is as following:

Private Sub SetAllFilesReadWrite(ByVal path As String)
Dim di As New DirectoryInfo(path)
For Each file As FileInfo In di.GetFiles()
If GetFileReadOnly(file.FullName) Then
SetFileReadOnly(file.FullName, False)
End If
Next
For Each dir As DirectoryInfo In di.GetDirectories()
SetAllFilesReadWrite(dir.FullName)
Next
End Sub

Simone Busoli on January 20, 2007 03:22 PM

Could you update it for VS2008?
It's turning the SLN files into "unknown VS version" on the VS version selector, and upon opening the cleaned solutions, a SCC message mentioning "Visual Studio 6.0" appears :S

Thanks!

Daniel Cazzulino on December 30, 2007 11:34 AM

I get the same Visual Studio 6.0 message as Daniel. Any ideas as to why?

Also, I've removed the source binding files from the config but it still unbinds me from TFS.

By the way, great tool, really useful, so thanks! :) I use it to take code home...get from TFS, zip, take home, back to work, rebind, "tfpt uu" + "tfpt online". Easy! (tfpt = Team Foundation Power Tools)

Sean Kearon on January 16, 2008 01:47 AM

Do you happen to have an update to .NET 2.0 or 3.5 by any chance? Possibly with a nice visual configuration dialog? :-)

Marc Scheuner on February 29, 2008 12:41 PM

Here is another request for a VS2008 update.

Alexey Romanov on March 16, 2008 01:43 PM

How about a custom MSBuild task that will do this? I'm thinking about giving it a try.

Richard Edwards on April 3, 2008 07:05 AM

Another request for vs2008 update.

Bryan on April 11, 2008 08:52 AM

Would also love to see an update to 2008, and the "no touch" version which creates the nice zip file with touching the original source.

Daniel McPherson on May 15, 2008 03:23 AM

Here is to VS2008!

Steve on June 12, 2008 12:20 PM

So, will there be a 2008 version?

Dave on June 16, 2008 08:01 AM

I've created a tool that will allow you to right-click on a project or solution in Windows Explorer and zip it up. It can also attach the zip file to an email using the users email client.

If you are interested, the project is called VS Project Zipper (source code is available). You can get more information about it from...

http://www.redwerb.com/files/folders/tools/entry118.aspx

Brian Brewder on July 7, 2008 12:36 AM

are you still maintaining this?

Double K on August 28, 2008 12:25 AM







(hear it spoken)


(no HTML)




Content (c) 2008 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.