Powershell to Rename Visual Studio Solution

Have you ever wanted to make a new Visual Studio solution (even a BizTalk one) as a copy of another, except just change the program names? And it would automatically rename everything, including the solutions, project files, assembly files and so on? I’ve needed this program for years, and finally wrote it!

Remember that the project name is often the Assembly name and the Default Namespace (in the AssemblInfo.cs file). This program renames the files, the folders, and updates the .sln and .csproj/.btproj files (and any other file). The “-exclude” parm on the Get-ChildItem is used to avoid binaries and other files that you do not want to change.

In BizTalk, the project name can also become your “TargetNamespace” for schemas.
Another trick with BizTalk is that some of the files are Unicode and some aren’t. So the rename program has to be conscious of that, and preserve the file type. I’m doing this by testing if the first tow characters of a file are are Byte Order Mark.

NOTE: Use with caution. Make a backup of your project before letting any program rip through and rename and change files!
Make sure your old/string new string are quite unique, so as not to change other random parts of your code. For example, if you use a 20-40 character project name like I have in $oldstring below, you are generally safe.

<pre>
cls 
$path = "d:\Git_Dev\MyProject" 
cd $path 
$oldstring = "MyCompany.BizTalk.System.OriginalName"
$newstring = "MyCompany.BizTalk.System.NewName"

$files = Get-ChildItem -Path $path *.* -rec -file -exclude obj,debug,bin,*.msi,*.exe,*.dll,*.snk 

$loopCounter = 1 

foreach ($file in $files) 
{
    ##(-join [char[]](Get-Content $file.PSPath -Encoding Byte -TotalCount 2)) -eq 'ÿþ'
    Write-Host "FileName: $($file.Fullname)" 
    $fileContents = Get-Content $file.FullName 
    $fileContentsUpdated = $fileContents -replace $oldstring, $newstring 

    if ($filecontents -ne $null) 
    {
        #Write-Host "---BEFORE----" 
        #Write-Host $fileContents 
        #Write-Host "---AFTER----" 
        #Write-Host $fileContentsUpdated 

        if ($fileContents -ne $fileContentsUpdated) 
        {
            if ($fileContents[0..2] -eq 'ÿþ') 
            {
                Write-Host "Updating File $($file.Name) [Unicode] "
                Set-Content -path $file.FullName -value $fileContentsUpdated -Encoding Unicode 
            }
            else 
            {
                Write-Host "Updating File $($file.Name) [NOT-Unicode] "
                Set-Content -path $file.FullName -value $fileContentsUpdated ## not Unicode 
            }
        }

        $fileNameUpdate = $file.Name -replace $oldstring, $newstring 
        if ($file.Name -ne $fileNameUpdate) 
        {
           Write-Host "Rename $($file.FullName) to $fileNameUpdate" 
           Rename-Item -Path $file.FullName -NewName $fileNameUpdate 
        }

        $loopCounter = $loopCounter + 1 
        if ($loopCounter -gt 2) 
          {
             #exit 
          }

   }
}

// Repeat again, to rename folders 
$files = Get-ChildItem -Path $path *.* -rec -directory -exclude obj,debug,bin

foreach ($file in $files) 
{

        $fileNameUpdate = $file.Name -replace $oldstring, $newstring 
        if ($file.Name -ne $fileNameUpdate) 
        {
           Write-Host "Rename $($file.FullName) to $fileNameUpdate" 
           Rename-Item -Path $file.FullName -NewName $fileNameUpdate 
        }

}

</pre>

Uncategorized  

Leave a Reply