From 511cd8a6b6a17582f06aa6b8779f9a128c1fb260 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Oct 2023 18:03:23 +0200 Subject: [PATCH 1/4] Rename-ChildItemsToAsciiRecursively: $Path parameter is no longer mandatory and initialized to current working directory instead --- .../Rename-ChildItemsToAsciiRecursively.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 index c76fe8d..4e095a3 100644 --- a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 +++ b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 @@ -2,8 +2,8 @@ { Param ( - [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)] - [string] $Path + [Parameter(ValueFromPipelineByPropertyName = $true, Position = 0)] + [string] $Path = (Get-Location).Path ) Process From ff3acbb574f8a9ab5e270849ab6a6ba7e84acb31 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Oct 2023 19:53:38 +0200 Subject: [PATCH 2/4] Fixing Rename-ChildItemsToAsciiRecursively when renaming a folder if the target name already exists --- .../Rename-ChildItemsToAsciiRecursively.psm1 | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 index 4e095a3..51b423c 100644 --- a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 +++ b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 @@ -8,7 +8,30 @@ Process { - Get-ChildItem $Path -Recurse | ForEach-Object { + Get-ChildItem $Path -Directory -Recurse | ForEach-Object { + $asciiName = [System.Text.Encoding]::ASCII.GetString([System.Text.Encoding]::ASCII.GetBytes($PSItem.Name)).Replace('?', '_') + + if ($PSItem.Name -eq $asciiName) { return } + + # If there's already a folder with the new name of the current one. + if (Test-Path (Join-Path $PSItem.Parent.FullName $asciiName)) + { + # Move the contents of the current folder to the other one with the new name. + Get-ChildItem $PSItem.FullName -Recurse | Move-Item -Destination (Join-Path $PSItem.Parent.FullName $asciiName) + + # Then delete the current folder. + Remove-Item $PSItem.FullName + } + else + { + Write-Verbose "Renaming `"$($_.FullName)`" to `"$asciiName`"." + $PSItem | Rename-Item -NewName $asciiName + + New-Object PSObject -Property @{ Original = $PSItem.FullName; Renamed = $asciiName } + } + } + + Get-ChildItem $Path -File -Recurse | ForEach-Object { $asciiName = [System.Text.Encoding]::ASCII.GetString([System.Text.Encoding]::ASCII.GetBytes($PSItem.Name)).Replace('?', '_') if ($PSItem.Name -eq $asciiName) { return } @@ -19,4 +42,4 @@ New-Object PSObject -Property @{ Original = $PSItem.FullName; Renamed = $asciiName } } } -} \ No newline at end of file +} From c684884b6840a1df39dfc2953f8cacb04c3b897b Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 18 Oct 2023 16:33:49 +0200 Subject: [PATCH 3/4] Code styling, DRY and output log improvements for Rename-ChildItemsToAsciiRecursively --- .../Rename-ChildItemsToAsciiRecursively.psm1 | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 index 51b423c..f5562fb 100644 --- a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 +++ b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 @@ -8,8 +8,10 @@ Process { + $pathLength = $Path.Length + 1 + Get-ChildItem $Path -Directory -Recurse | ForEach-Object { - $asciiName = [System.Text.Encoding]::ASCII.GetString([System.Text.Encoding]::ASCII.GetBytes($PSItem.Name)).Replace('?', '_') + $asciiName = Get-AsciiName($PSItem.Name) if ($PSItem.Name -eq $asciiName) { return } @@ -21,25 +23,32 @@ # Then delete the current folder. Remove-Item $PSItem.FullName - } - else - { - Write-Verbose "Renaming `"$($_.FullName)`" to `"$asciiName`"." - $PSItem | Rename-Item -NewName $asciiName - New-Object PSObject -Property @{ Original = $PSItem.FullName; Renamed = $asciiName } + return } + + Write-Verbose "Renaming '$($PSItem.FullName)' to '$asciiName'." + + $PSItem | Rename-Item -NewName $asciiName + + New-Object PSObject -Property @{ Original = $PSItem.FullName.Substring($pathLength); Renamed = $asciiName } } Get-ChildItem $Path -File -Recurse | ForEach-Object { - $asciiName = [System.Text.Encoding]::ASCII.GetString([System.Text.Encoding]::ASCII.GetBytes($PSItem.Name)).Replace('?', '_') + $asciiName = Get-AsciiName($PSItem.Name) if ($PSItem.Name -eq $asciiName) { return } - Write-Verbose "Renaming `"$($_.FullName)`" to `"$asciiName`"." + Write-Verbose "Renaming '$($PSItem.FullName)' to '$asciiName'." + $PSItem | Rename-Item -NewName $asciiName - New-Object PSObject -Property @{ Original = $PSItem.FullName; Renamed = $asciiName } + New-Object PSObject -Property @{ Original = $PSItem.FullName.Substring($pathLength); Renamed = $asciiName } } } } + +function Get-AsciiName([string] $name) +{ + return [System.Text.Encoding]::ASCII.GetString([System.Text.Encoding]::ASCII.GetBytes($name)).Replace('?', '_') +} From f82a0c307d61f461aef17a81540d452d6caaef65 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 20 Oct 2023 10:28:34 +0200 Subject: [PATCH 4/4] Adding verbose message when moving the contents of a folder instead of renaming --- .../Rename-ChildItemsToAsciiRecursively.psm1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 index f5562fb..7435852 100644 --- a/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 +++ b/Utilities/Rename-ChildItemsToAsciiRecursively/Rename-ChildItemsToAsciiRecursively.psm1 @@ -18,6 +18,8 @@ # If there's already a folder with the new name of the current one. if (Test-Path (Join-Path $PSItem.Parent.FullName $asciiName)) { + Write-Verbose "Moving the contents of '$($PSItem.FullName)' to '$asciiName'." + # Move the contents of the current folder to the other one with the new name. Get-ChildItem $PSItem.FullName -Recurse | Move-Item -Destination (Join-Path $PSItem.Parent.FullName $asciiName)