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

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
<May 2008>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Categories

Powered by: newtelligence dasBlog 1.8.5223.1