Francesco's blog

 Wednesday, December 14, 2005

In a previous post I introduced a tiny utility to clean up Visual Studio projects. I have introduced minor fixes to it since then, so this is the most recent version, which also deletes *.suo and *.user files and produces a report of all the files and folders that couldn't be deleted for any reason:

Imports System.IO

Module Module1
  
Sub Main(ByVal args() As String)
      Console.WriteLine(
"VSProjCleaner tool (C) 2005 Francesco Balena, Code Archirects")
     
If args.Length = 0 Then
        
Console.WriteLine("Removes BIN and OBJ folders, .suo and .user files from Visual Studio projects")
         Console.WriteLine()
         Console.WriteLine(
" SYNTAX: VSProjCleaner dirname")
         Console.WriteLine()
        
Return
     
End If
     
Console.WriteLine()

      ' Use current directory if no argument has been specified
     
Dim rootDir As String = Directory.GetCurrentDirectory()
     
If args.Length > 0 Then rootDir = args(0)
     
' Read all the folder names in the specified directory tree
     
Dim dirNames() As String = Directory.GetDirectories(rootDir, "*.*", SearchOption.AllDirectories)
     
Dim errorsList As New List(Of String)

      ' delete any .suo and vbproj.user file
     
For Each dir As String In dirNames
        
Dim files As New List(Of String)
         files.AddRange(Directory.GetFiles(dir,
"*.suo"))
         files.AddRange(Directory.GetFiles(dir,
"*.user"))
        
For Each fileName As String In files
           
Try
              
Console.Write("Deleting {0} ...", fileName)
               File.Delete(fileName)
               Console.WriteLine(
"DONE")
            Catch ex As Exception
               Console.WriteLine()
               Console.WriteLine(
" ERROR: {0}", ex.Message)
               errorsList.Add(fileName &
": " & ex.Message)
            
End Try
         
Next
      
Next

      ' Delete all the BIN and OBJ subdirectories
      
For Each dir As String In dirNames
         
Dim dirName As String = Path.GetFileName(dir).ToLower()
         
If dirName = "bin" OrElse dirName = "obj" Then
            
Try
               
Console.Write("Deleting {0} ...", dir)
               Directory.Delete(dir,
True)
               Console.WriteLine(
"DONE")
            
Catch ex As Exception
               Console.WriteLine()
               Console.WriteLine(
" ERROR: {0}", ex.Message)
               errorsList.Add(dir &
": " & ex.Message)
            
End Try
         
End If
      
Next
      
Console.WriteLine()
      Console.WriteLine(
New String("-"c, 60))
      
If errorsList.Count = 0 Then
         
Console.WriteLine("All directories and files were removed successfully")
      
Else
         
Console.WriteLine("{0} directories or directories couldn't be removed", errorsList.Count)
         Console.WriteLine(
New String("-"c, 60))
         
For Each msg As String In errorsList
            Console.WriteLine(msg)
         
Next
      
End If
   
End Sub
End
Module

You can download the binary version here: VSProjCleaner.exe (24 KB) 
or the complete solution here: VSProjCleaner_Source.zip (15.08 KB)

Using this tool couldn't be simpler: just run it from the command window and pass a folder name as an argument, enclosing it in double quotes if it contains spaces:
                            VSPROJCLEARNER "c:\my projects\testproj"
The tool recursively visits all the folders and deletes all BIN and OBJ directories, and deletes *.suo and *.user files. At the end of the process it lists all the folders and files that couldn't be removed, if any.

DISCLAIMER: I have tested this code against my own projects and it always worked correctly, but I don't should be held responsible for any loss of important files (for example, a .user file that has nothing to do with a Visual Studio project.). For this reason, I urge you to apply this tool only to a COPY of your projects

3/23/2009 11:47:59 AM (GMT Standard Time, UTC+00:00)
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Elaina

http://www.freearticletrove.com
10/29/2009 1:20:06 PM (GMT Standard Time, UTC+00:00)
Thanks - I do a similar thing in perl with:

#perl
use File::Find;
use File::Path;
@dirsToIgnore = (".git","_vti_.*", "App_Code", "includes", "images", "includes");
@dirsToDelete = ("bin","obj");
@filesToDelete = ("\.suo","\.user");
finddepth(sub{
($dir,$file) = ($File::Find::dir,$_);
foreach $dirToIgnore (@dirstoignore){
return if $dir =~ /$dirToIgnore/;
}
foreach $dirToDelete (@dirsToDelete){
if ("$dir/" =~ /\/$dirToDelete\//){
unlink("$dir/$file");
}
}
foreach $fileToDelete (@filesToDelete){
if ($file =~ /$fileToDelete/){
unlink("$dir/$file");
print "Deleted File: $dir/$file\n";
}
}
rmdir;
print "Deleted Dir: $dir $!\n"; # if ($! eq "");
},$ARGV[0])
10/29/2009 4:04:13 PM (GMT Standard Time, UTC+00:00)
Sorry, posted wrong version:

#perl
use File::Find;
use File::Path;
@dirsToIgnore = (".git","_vti_.*", "App_Code", "includes", "images", "photo");
@dirsToDelete = ("bin","obj");
@filesToDelete = ("\.suo","\.user");
my @log;
finddepth(sub{
($dir,$file) = ($File::Find::dir,$_);
$dir =~ s/\\/\//;
foreach $dirToIgnore (@dirstoignore){
return if $dir =~ /$dirToIgnore/;
}
foreach $dirToDelete (@dirsToDelete){
if ("$dir/" =~ /\/$dirToDelete\//){
unlink("$dir/$file");
push @log, "Deleted: $dir/$file\n";
last;
}
}
foreach $fileToDelete (@filesToDelete){
if ($file =~ /$fileToDelete/){
unlink("$dir/$file");
push @log, "Deleted: $dir/$file\n";
last;
}
}
push @log, "Deleted: $dir/$file \n" if (rmdir "$dir/$file" && $! !~ /directory/i );
},$ARGV[0]);
sort @log;
print @log;
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

 
Get RSS/Atom Feed
RSS 2.0 | Atom 1.0
Search in the blog
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Categories

Powered by: newtelligence dasBlog 1.8.5223.1