Cleaning Source Control Bindings from Source Code With Powershell

I needed to prepare some code for release to someone outside our company. The code was a Visual Studio 2005 solution and I don’t have VS2005 installed any more. This made removing the source control bindings and debug files a pain as I couldn’t just open the solution and use Visual Studio to do it.

So instead, I came up with the following powershell code to remove the source control bindings and remove unnecessary files, which worked quite nicely:


# Remove unnecessary files  
get-childitem . -include *.vssscc,*.user,*.vspscc,*.pdb,Debug -recurse |   
    %{   
        remove-item $_.fullname -force -recurse   
    }  

# Remove the bindings from the sln files  
get-childitem . -include *.sln -recurse |   
    %{   
        $file = $_;   
        $inVCSection = $False;  
        get-content $file |   
        %{   
            $line = $_.Trim();   
            if ($inVCSection -eq $False -and $line.StartsWith('GlobalSection') -eq $True -and $line.Contains('VersionControl') -eq $True) {   
                $inVCSection = $True   
            }   
            if ($inVCSection -eq $False) {   
                add-content ($file.fullname + '.new') $_   
            }   
            if ($inVCSection -eq $True -and $line -eq 'EndGlobalSection') {   
                $inVCSection = $False  
            }  
        }  
        mv ($file.fullname + '.new') $file.fullname -force   
    }  

# Remove the bindings from the csproj files  
get-childitem . -include *.csproj -recurse |   
    %{   
        $file = $_;   
        get-content $file |   
        %{   
            $line = $_.Trim();   
            if ($line.StartsWith('<Scc') -eq $False) {  
                add-content ($file.fullname + '.new') $_   
            }  
        }  
        mv ($file.fullname + '.new') $file.fullname -force   

    }
 

Comments

  1. Luiz says:

    I had the same issue (Jorge you likely have large files, but not that many of them that is a key diefnrefce). The problem is that Get-ChildItem returns a full object for each file that you are enumerating in the folder. Thus, with 500k, you are requesting a lot of information.The solution is to NOT use Get-ChildItem and use [io.directoryinfo]Try this: (NOTE: AS IS and scrapped together from a known working script of mine the idea is valid if it doesn’t work )######## COPY START ########$SrcFolder = \\your.path\to\lots\of\files\ #can be any type of directory$Fileage = -14 #Number of days you want to RETAIN (ie it will delete files older than this many days)#set up directory info[io.directoryinfo]$dir = $SrcFolder#Get the files that are older than your $fileage$FileCol = ($dir.GetFiles() | Where {$_.LastWriteTime -lt (Get-Date).AddDays($Fileage)}) #If your source folder may contain subfolders use:#Where {!$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays($Fileage)})#if you want a progress bar (when running interactively) uncomment the following two lines#$i=1#$Files2Remove = $FileCol.Count#Get your filesForeach($File in $FileCol){#if you want a progress bar uncomment the following line#Write-Progress -Activity Removing files older than $([system.Math]::Abs($Fileage)) days -id 1 -status ( Files Processed $i of +$Files2Remove.ToString()) -percentComplete ($i / $Files2Remove*100)if(test-path $File.FullName){remove-item -path $File.FullName -Force -WhatIf #Comment the -Whatif to actually remove the files}#if you want a progress bar uncomment the following line#$i++}######## COPY END ########Hope that works better for you with 500k files nothing is going to be all that speedy. If you’d like a quick comparison test (between gci and io.directoryinfo) run these to get your file count:[io.directoryinfo]$dir = $SrcFoldermeasure-command {$dir.getFiles()).count}Measure-command {(Get-ChildItem $SrcFolder).Count} #you may have to kill this one after a while, but you should get the idea that it is slower.