Project Source Code Removal

The other day I ran into a slight problem.

It began simply enough — I was to deploy a complex asp.net project to a client server without any source code. The intellectual property of the company I worked for had to be maintained.

Simple enough, I thought. Just create a deployment package and specify only the content files.

However for some reason the deployment package refused to create the virtual directory on the IIS of the target server, and several phone calls trying to diagnose the issue yielded precisely nothing.

With irate clients on the line I had to think on my feet.

My  idea was as follows:

  1. Admin at the client to create a virtual directory for me
  2. Self to manually remove our intellectual property

There was only one drawback — the solution has dozens and dozens of sub folders and it would be agony to manually delete all the code, schema, solution, project files and so on.

Since computers are here to work for us, I thought I’d whip together a small application do to this for me.

Design

  1. Application should know in advance what files to delete
  2. The algorithm must be recursive, since we don’t know how deep in the folder structure the files are
  3. Application should NOT delete by default. This is a potentially dangerous application that could nuke all the code on your disk! You must specify delete explicitly
  4. Application will print all the files it processes

Code

The first thing the code does is outline the extensions to process

//
// The extensions we want to remove 
//
static string[] extensions = {“cs”,“xsx”,“xsd”,
    “resx”,“rpt”,“csproj”,“user”,“projdata”,
    “sln”,“vb”,“scc”,“pdb”,“obj”,
    “vssscc”,“vbproj”,
    “suo”};

The next order of business is the recursive procedure, ProcessFiles. This function

  1. Processes any files in the current folder
  2. Checks if there are any subfolders
  3. Recursively calls ProcessFiles on each of these

 

/// <summary>
/// Processes the files in the current folder
/// </summary>
/// <param name=”folder”>The folder to process</param>
/// <param name=”delete”>if set to <c>true</c> Delete the files.</param>
static void ProcessFiles(string folder, bool delete)
{
    //
    // Increment the folder count
    //
    foldersProcessed++;
    //
    // Get the directory information
    //
    DirectoryInfo di = new DirectoryInfo(folder);
    FileInfo[] matches = null;
    //
    // For each extesion type in our array,
    // perform the cleanup
    //
    foreach (string s in extensions)
    {
        //
        // Get the matching files
        //
        matches = di.GetFiles(string.Format(“*.{0}”,s));
        foreach(FileInfo f in matches)
        {
            //
            // Increment the file count
            //
            filesProcessed++;
            if (!delete)
            {
                //
                // If delete was not specified, just print the file name
                //
                Console.WriteLine(f.Name);
            }
            else
            {
                try
                {
                    Console.WriteLine(“Deleting {0}”, f.Name);
                    //
                    // Do the delete
                    //
                    File.Delete(f.FullName);
                }
                catch (System.IO.FileNotFoundException)
                {
                    //
                    // The file is somehow no longer available
                    //
                    Console.WriteLine(“Could not find {0}!”, f.Name);
                }
                catch (System.UnauthorizedAccessException)
                {
                    //
                    // Access to the file is somehow denied; either it does
                    // not belong to the current user or it is readonly
                    //
                    Console.WriteLine(“Could not delete: Access denied {0}!”, f.Name);
                }
                catch (Exception ex)
                {
                    //
                    // Could not delete
                    //
                    Console.Write(“Could not delete {0}{1}Error: {2}?”,
                        f.Name, Environment.NewLine, ex.Message);
                }
            }
        }
        //
        // Ouput information of files deleted
        //
        if(matches.Length>0×0)
            Console.WriteLine(“{0} found in {1}{0}”,
                matches.Length,
                folder,
                Environment.NewLine);
    }
    //
    // Check if there are any directories in this directory
    //
    DirectoryInfo[] dirs = di.GetDirectories();
    if(dirs.Length>0×0)
    {
        foreach(DirectoryInfo di2 in dirs)
        {
            //
            // Process the directories
            //
            ProcessFiles(di2.FullName, delete);
        }
    }
}

The interesting bit is in the delete section, to be specific where we trap the UnauthorizedAccess Exception.

I added this bit because the asp.net project was under the source code control of VisualSourceSafe which sets the attributes of checked in files to readonly. So if you tried to delete such a file it would raise an exception.

Design Choices

  1. Why not programmatically remove the attribute? Because the onus on the person running the utility is to ensure the project is ready for deployment. This means the folder being processes SHOULD NOT be the one with the actual code.
  2. Why not delete the debug and temp folders? Because you may have a good reason for generating output in the temp folder. I can’t read your mind which one you want!

Room for Improvement

  1. Store the file extensions in the registry or in a config file and write another utility to add and remove items
  2. More robust handling of the passed parameters
  3. Allow specification of the desired build debug|release|custom and clean out the rest
  4. Additional parameter to backup the code before deleting

Screen Shots

Development in the IDE

Running Cleanup

Running Cleanup and specifying a folder

Running Cleanup on a root folder

Running Cleanup on a VisualSourceSafe project

Running Cleanup on a deployment ready project

Give it a go. Download The console application and knock yourself out. You can also download the source code and see how it works, and modify it for your needs

Legalese

I WILL NOT take any responsibility if you manage to delete all the source code on your PC.

kick it on DotNetKicks.com

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blogmarks
  • co.mments
  • del.icio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • TailRank
  • YahooMyWeb
 

Other posts

One response


  1. Looks helpful. Since you’ve been playing with PowerShell lately, I thought you might enjoy the PowerShell version:

    ## Define the extensions
    $extensions = “*.cs”,”*.xsx”,”*.xsd”, “*.resx”,”*.rpt”,”*.csproj”,”*.user”,”*.projdata”,
    “*.sln”,”*.vb”,”*.scc”,”*.pdb”,”*.obj”,”*.vssscc”,”*.vbproj”,”*.suo”

    ## Show what would happen if they actually did the operation (doesn’t actually delete)
    dir * -include $extensions -rec | remove-item -whatif

    ## Actually delete. Has a typo on purpose :)
    dir * -include $extensions -rec | remove/item

    ## Actually delete, and display verbose information. Has a typo on purpose :)
    dir * -include $extensions -rec | remove/item -Verbose

    ## Actually delete, even when files have been made ReadOnly. Has a typo on purpose :)
    dir * -include $extensions -rec | remove/item -force

    The -WhatIf and -Verbose parameters (just two of our “common parameters”) are two features that make life as an admin so much easier. There are others — you should check them out!

    Lee

Leave a Reply