Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- )
- # Translitterate function http://j.mp/10YwkXP
- function OC_translitterate {
- param(
- [string]$inputString
- )
- [string]$formD = $inputString.Normalize(
- [System.text.NormalizationForm]::FormD
- )
- $stringBuilder = 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){
- $stringBuilder.Append($formD[$i]) | Out-Null
- }
- }
- $string = $stringBuilder.ToString().Normalize([System.text.NormalizationForm]::FormC)
- return $string
- }
- # 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 = OC_translitterate $_)) {
- 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 = OC_translitterate $_)) {
- 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!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement