Last active
November 7, 2015 21:16
-
-
Save vadyua/b0963b117d0218fd3877 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,HelpMessage="Provide a full path to files")] | |
# [ValidateScript({Test-Path $_ -PathType 'Container'})] | |
[alias("p")] | |
[System.String]$path | |
) | |
$similar = @{ | |
[char]'á' = "a" | |
[char]'à' = "a" | |
[char]'â' = "a" | |
[char]'ä' = "a" | |
[char]'å' = "a" | |
[char]'ã' = "a" | |
[char]'æ' = "ae" | |
[char]'ç' = "c" | |
[char]'é' = "e" | |
[char]'è' = "e" | |
[char]'ê' = "e" | |
[char]'ë' = "e" | |
[char]'í' = "i" | |
[char]'î' = "i" | |
[char]'ï' = "i" | |
[char]'ì' = "i" | |
[char]'ó' = "o" | |
[char]'ô' = "o" | |
[char]'ò' = "o" | |
[char]'ö' = "o" | |
[char]'ø' = "oe" | |
[char]'õ' = "o" | |
[char]'œ' = "oe" | |
[char]'ð' = "o" | |
[char]'ú' = "u" | |
[char]'ü' = "u" | |
[char]'ù' = "u" | |
[char]'û' = "u" | |
[char]'ý' = "y" | |
[char]'ÿ' = "y" | |
[char]'Á' = "A" | |
[char]'À' = "A" | |
[char]'Â' = "A" | |
[char]'Ä' = "A" | |
[char]'Å' = "A" | |
[char]'Ã' = "A" | |
[char]'Æ' = "AE" | |
[char]'É' = "E" | |
[char]'È' = "E" | |
[char]'Ê' = "E" | |
[char]'Ë' = "E" | |
[char]'Í' = "I" | |
[char]'Ì' = "I" | |
[char]'Î' = "I" | |
[char]'Ï' = "I" | |
[char]'Ó' = "O" | |
[char]'Ö' = "O" | |
[char]'Ô' = "O" | |
[char]'Ò' = "O" | |
[char]'Õ' = "O" | |
[char]'Ø' = "Oe" | |
[char]'Œ' = "Oe" | |
[char]'Ú' = "U" | |
[char]'Ù' = "U" | |
[char]'Û' = "U" | |
[char]'Ü' = "U" | |
[char]'Ý' = "Y" | |
[char]'Ÿ' = "Y" | |
[char]'Ð' = "D" | |
[char]'Ć' = "C" | |
[char]'ć' = "c" | |
[char]'Ǵ' = "G" | |
[char]'ǵ' = "ǵ" | |
[char]'Ḱ' = "K" | |
[char]'ḱ' = "k" | |
[char]'Ĺ' = "L" | |
[char]'Ḿ' = "M" | |
[char]'ḿ' = "m" | |
[char]'Ń' = "N" | |
[char]'ń' = "n" | |
[char]'Ñ' = "N" | |
[char]'ñ' = "n" | |
[char]'Ő' = "O" | |
[char]'ő' = "ő" | |
[char]'Ṕ' = "P" | |
[char]'ṕ' = "p" | |
[char]'Ŕ' = "R" | |
[char]'ŕ' = "r" | |
[char]'Ś' = "S" | |
[char]'ś' = "s" | |
[char]'Ű' = "U" | |
[char]'ű' = "u" | |
[char]'Ź' = "Z" | |
[char]'ź' = "z" | |
[char]'þ' = "p" | |
[char]'Þ' = "p" | |
[char]'ß' = "sz" | |
[char]'ẞ' = "SZ" | |
} | |
# http://stackoverflow.com/a/7840951 | |
function Replace-Diacritics { | |
param( | |
[string]$src = [String]::Empty | |
) | |
$out = "" # non-formal normalize | |
foreach ($c in $src.ToCharArray()) { | |
if ($similar[$c] -cne $Null) { | |
$out += $similar[$c] | |
} else { | |
$out += $c | |
} | |
} | |
[string]$formD = $out.Normalize( | |
[System.text.NormalizationForm]::FormD | |
) | |
$sb = new-object System.Text.StringBuilder | |
for ($i = 0; $i -lt $formD.Length; $i++){ | |
$unicodeCategory = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($formD[$i]) | |
$nonSPacingMark = [System.Globalization.UnicodeCategory]::NonSpacingMark | |
if($unicodeCategory -ne $nonSPacingMark){ | |
$sb.Append($formD[$i]) | Out-Null | |
} | |
} | |
$postnormalize = $sb.ToString().Normalize([System.text.NormalizationForm]::FormC) | |
# additional "Cyrillic" postnormalize | |
return [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding(1251).GetBytes($postnormalize)) | |
} | |
# Total counter | |
$total = 0 | |
# Rename folders | |
Get-ChildItem -LiteralPath $path -Recurse -Force -EA 0 | ? {$_.psIsContainer -eq $true} | Sort @{expression = {$_.Fullname.length}} -descending | %{ | |
if($_.Name -ne ($new = Replace-Diacritics $_.Name) -or $_.Name.Length -ne $new.Length) { | |
Write-Host "Rename folder:" $_.Fullname | |
Move-Item -Force -LiteralPath $_.Fullname -Destination (Join-Path ($_.Fullname | Split-Path -Parent) -Childpath $new) | |
$total++ | |
} | |
} | |
# Rename files | |
gci -LiteralPath $path -Recurse -Force | Where {$_.psIsContainer -eq $false} | %{ | |
if($_.Name -ne ($new = Replace-Diacritics $_.Name) -or $_.Name.Length -ne $new.Length) { | |
Write-Host "Rename file: " $_.Fullname | |
Move-Item -LiteralPath $_.Fullname -Destination (Join-Path ($_.Fullname | Split-Path -Parent) -Childpath $new) | |
$total++ | |
} | |
} | |
Write-Host -ForegroundColor green "Total $total items renamed!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment