Tuesday, May 22, 2012

Cleaning up after VS projects builds

Visual Studio seems to create large amounts of temporary files which over time occupy gigs of harddrive space.
Here's a little python script that removes many of these files recursively.

 import os, re  
 ext_list = ["obj", "idb", "manifest", "pdb", "ncb", "suo", "pch", "pchi", "sdf", "embed.manifest", "intermediate.manifest", "embed.manifest.res", "res", "dep"]      
 total_size_bytes = 0  
 for dirname, dirnames, filenames in os.walk('.'):  
   for filename in filenames:  
         for ext in ext_list:  
             expr = "^[a-zA-Z0-9\s]+." + ext + "$"   
             matcher = re.compile(expr)  
             if matcher.match(filename):  
                 statinfo = os.stat(os.path.join(dirname, filename))  
                 total_size_bytes += statinfo.st_size  
                 print "Deleting: " + os.path.join(dirname, filename) + " " + str(statinfo.st_size) + " Bytes"  
                 os.remove(os.path.join(dirname, filename))  
 print "Total: " + str(total_size_bytes/1024) + " KB (" + str(total_size_bytes) + " Bytes)"  
 print "Total: " + str(total_size_bytes/1024/1024) + " MB (" + str(total_size_bytes/1024/1024/1024) + " GB)"  

Pipe the python script to a text file to keep track of which files are deleted.

 cleanup.py > cleanup.txt  

Adapt the file extensions as required.

* Code formatted by http://codeformatter.blogspot.de/