PowerShell Recursive Rename for an SVN directory

On a large repository, I was attempting to rename the SVN tracking folders that are nested at every directory level, I needed to do this because of a difference in the leading character ‘.’ (period) vs ‘_’ (underscore). I know this could have easily been resolved with a new fetch but I wanted to avoid a lengthy download over a VPN connection.

I thought I would just quickly list some PowerShell commands I was playing with to clean up the repository as a blog post.

The closest I got to a solution but with a lot of errors/warnings during the process was:

Get-ChildItem * -Recurse -force | Where-Object { $_.Mode -eq "d--h-" } | Rename-Item -force -newname '_svn'

It seems the -force parameter was required. I’m not sure why it errors but it still works. Further investigation would be around how many times the commands run per directory, possibly too many times. Another avenue for investigation is the -silent parameter but that’s likely only going to obscure any issues.

Just for reference here’s what else I tried, these did not succeed.

Get-Childitem -path . -include .svn -recurse | Rename-Item -newname {$_.name -replace '.svn','_svn'}
Get-Childitem -path . -include .svn -recurse | foreach { Rename-Item .svn _svn }
Get-Childitem -path . -recurse -include '.svn' | foreach { Rename-Item .svn _svn }
Get-Childitem -path . -recurse | rename-item -newname { $_.name -replace '.svn','_svn' }
Get-Childitem -path . -recurse -include .svn | move-item -destination _svn

If you’re a PowerShell expert feel free to correct my possibly misguided attempt at a recursive rename.

Update 29th Dec 2011
I stumbled upon someone much more clever undertaking a similar rename process. In this case jQuery text, but the logic serves the same purpose it goes and renames content of the files that logic can be replaced with the move-item command.

$find = 'jquery-1\.4\.4'
$replace = 'jquery-1\.5\.1'
$match = '*.cshtml' , '*.vbhtml'
$preview = $true

foreach ($sc in dir -recurse -include $match | where { test-path $_.fullname -pathtype leaf} ) {
    select-string -path $sc -pattern $find
    if (!$preview) {
       (get-content $sc) | foreach-object { $_ -replace $find, $replace } | set-content $sc
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s