Advertisement
vady

translitterate-lite.ps1

May 2nd, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Param(
  2.    [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,HelpMessage="Provide a full path to files")]
  3.    [ValidateScript({Test-Path $_ -PathType 'Container'})]
  4.    [alias("p")]
  5.    [System.String]$path
  6. )
  7.  
  8. # Translitterate function http://j.mp/10YwkXP
  9. function OC_translitterate {
  10.     param(
  11.         [string]$inputString
  12.     )
  13.     [string]$formD = $inputString.Normalize(
  14.             [System.text.NormalizationForm]::FormD
  15.     )
  16.     $stringBuilder = new-object System.Text.StringBuilder
  17.     for ($i = 0; $i -lt $formD.Length; $i++){
  18.         $unicodeCategory = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($formD[$i])
  19.         $nonSPacingMark = [System.Globalization.UnicodeCategory]::NonSpacingMark
  20.         if($unicodeCategory -ne $nonSPacingMark){
  21.             $stringBuilder.Append($formD[$i]) | Out-Null
  22.         }
  23.     }
  24.     $string = $stringBuilder.ToString().Normalize([System.text.NormalizationForm]::FormC)
  25.     return $string
  26. }
  27.  
  28. # Total counter
  29. $total = 0
  30.  
  31. # Rename folders
  32. Get-ChildItem -LiteralPath $path -Recurse -Force -EA 0 | ? {$_.psIsContainer -eq $true} | Sort @{expression = {$_.Fullname.length}} -descending | %{
  33.     if($_.Name -ne ($new = OC_translitterate $_)) {
  34.         Write-Host "Rename folder:" $_.Fullname
  35.         Move-Item -Force -LiteralPath $_.Fullname -Destination (Join-Path ($_.Fullname | Split-Path -Parent) -Childpath $new)
  36.         $total++
  37.     }
  38. }
  39.  
  40. # Rename files
  41. gci -LiteralPath $path -Recurse -Force | Where {$_.psIsContainer -eq $false} | %{
  42.     if($_.Name -ne ($new = OC_translitterate $_)) {
  43.         Write-Host "Rename file: " + $_.Fullname
  44.         Move-Item -LiteralPath $_.Fullname -Destination (Join-Path ($_.Fullname | Split-Path -Parent) -Childpath $new)
  45.         $total++
  46.     }
  47. }
  48.  
  49. Write-Host -ForegroundColor green "Total $total items renamed!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement