Skip to content

Instantly share code, notes, and snippets.

@danmalcolm
Last active August 29, 2015 14:19
Show Gist options
  • Save danmalcolm/fbd7c961c3e7ebe482de to your computer and use it in GitHub Desktop.
Save danmalcolm/fbd7c961c3e7ebe482de to your computer and use it in GitHub Desktop.
# To run:
# 1. Copy this script somewhere and amend the variables
# 2. Open Powershell command prompt
# 3. Run: set-executionpolicy unrestricted -scope process -force
# 4. "dot-source" this script, e.g. ./rename.ps1
$old_namespace = "My.Old.Namespace"
$new_namespace = "My.New.Namespace"
$solution_folder = "C:\Users\Dan\Documents\GitHub\ExampleProject\src"
# 1. Remove bin / obj folders
get-childItem $solution_folder -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }
# 2. Rename folders and files
function rename-items($folder)
{
foreach ($item in get-childitem $folder)
{
$item_name = $item.FullName
if($item.FullName.Contains($old_namespace))
{
$new_name = $item.FullName.Replace($old_namespace, $new_namespace)
write-host "Renaming $item"
write-host "Renaming to $new_name"
rename-item $item_name $new_name
#$cmd = 'svn mv "' + $item_name + '" "' + $new_name + '"'
#iex $cmd
#exit
$item_name = $new_name
}
if($item.PSIsContainer)
{
rename-items $item_name
}
}
}
rename-items $solution_folder
# 3. Replace content in all source files (this will also update any paths in solution and project files in line with step 2)
$src_files = get-childitem $solution_folder -recurse -include *.config,*.cs,*.cshtml,*.csproj,*.sln,*.targets | where { !$_.PSIsContainer }
foreach ($src_file in $src_files)
{
write-host "Replacing content in file: $src_file"
(Get-Content $src_file.FullName) | % { $_ -replace $old_namespace, $new_namespace } | Set-Content -path $src_file.FullName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment