From 642edffcfea2452f9463912475b13df39e185bb0 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Sat, 25 May 2024 21:11:34 -0300 Subject: [PATCH] update script to be invoked only in folder where you want to remove version from folders --- RemoveVersion/RemoveVersion.ps1 | 109 +++++++++++++++++--------------- 1 file changed, 58 insertions(+), 51 deletions(-) diff --git a/RemoveVersion/RemoveVersion.ps1 b/RemoveVersion/RemoveVersion.ps1 index b615ff2..c7a9345 100644 --- a/RemoveVersion/RemoveVersion.ps1 +++ b/RemoveVersion/RemoveVersion.ps1 @@ -1,59 +1,66 @@ # Get each folder in the current directory $folders = Get-ChildItem -Directory + +# Array of arrays to store work queue +$work = @() + foreach ($folder in $folders) { - # Get each folder in the folder - $new_folders = Get-ChildItem -Path $folder.FullName -Directory - foreach ($folder in $new_folders) { - # Find the version number in the folder name - $version = $folder.Name -match '(\d+\.\d+\.\d+\.\d+)' - if ($version) { - # Replace entire folder name with the version number - $new_name = $matches[0] + # Find the version number in the folder name + $version = $folder.Name -match '(\d+\.\d+\.\d+\.\d+)' + if ($version) { + # Replace entire folder name with the version number + $new_name = $matches[0] - # if version has a alpha character, remove it, eg 0.1.0a, then re-add it to new name - $alpha = $folder.Name -match '(\d+\.\d+\.\d+\.\d+)([a-zA-Z])' - if ($alpha) { - $new_name = $matches[1] - $new_name = $new_name + $matches[2] - } - - #Rename-Item -Path $folder.FullName -NewName $new_name - Write-Host "Renamed $folder to $new_name" - continue + # if version has a alpha character, remove it, eg 0.1.0a, then re-add it to new name + $alpha = $folder.Name -match '(\d+\.\d+\.\d+\.\d+)([a-zA-Z])' + if ($alpha) { + $new_name = $matches[1] + $new_name = $new_name + $matches[2] } - # Find the version number in the folder name - $version = $folder.Name -match '(\d+\.\d+\.\d+)' - if ($version) { - # Replace entire folder name with the version number - $new_name = $matches[0] + $toadd = @($folder.FullName, $new_name) + $work += $toadd - # if version has a alpha character, remove it, eg 0.1.0a, then re-add it to new name - $alpha = $folder.Name -match '(\d+\.\d+\.\d+)([a-zA-Z])' - if ($alpha) { - $new_name = $matches[1] - $new_name = $new_name + $matches[2] - } - - #Rename-Item -Path $folder.FullName -NewName $new_name - Write-Host "Renamed $folder to $new_name" - continue - } - # Find the version number in the folder name - $version = $folder.Name -match '(\d+\.\d+)' - if ($version) { - # Replace entire folder name with the version number - $new_name = $matches[0] - - # if version has a alpha character, remove it, eg 0.1.0a, then re-add it to new name - $alpha = $folder.Name -match '(\d+\.\d+)([a-zA-Z])' - if ($alpha) { - $new_name = $matches[1] - $new_name = $new_name + $matches[2] - } - - #Rename-Item -Path $folder.FullName -NewName $new_name - Write-Host "Renamed $folder to $new_name" - continue - } + Write-Host "Rename $folder to $new_name" + continue } + # Find the version number in the folder name + $version = $folder.Name -match '(\d+\.\d+\.\d+)' + if ($version) { + # Replace entire folder name with the version number + $new_name = $matches[0] + + # if version has a alpha character, remove it, eg 0.1.0a, then re-add it to new name + $alpha = $folder.Name -match '(\d+\.\d+\.\d+)([a-zA-Z])' + if ($alpha) { + $new_name = $matches[1] + $new_name = $new_name + $matches[2] + } + $toadd = @($folder.FullName, $new_name) + $work += $toadd + + Write-Host "Rename $folder to $new_name" + continue + } + # Find the version number in the folder name + $version = $folder.Name -match '(\d+\.\d+)' + if ($version) { + # Replace entire folder name with the version number + $new_name = $matches[0] + + # if version has a alpha character, remove it, eg 0.1.0a, then re-add it to new name + $alpha = $folder.Name -match '(\d+\.\d+)([a-zA-Z])' + if ($alpha) { + $new_name = $matches[1] + $new_name = $new_name + $matches[2] + } + $toadd = @($folder.FullName, $new_name) + $work += $toadd + + Write-Host "Rename $folder to $new_name" + continue + } +} +Read-Host -Prompt "Press Enter to continue" +foreach ($item in $work) { + Rename-Item -Path $item[0] -NewName $item[1] } \ No newline at end of file