Can folders be automatically merged using the following Applet:

Dan10

New Member
Joined
Nov 26, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Dir | Rename-Item -NewName { $_.name -replace "[0-9]",”” }

Only I have a lot of Folder names with numbers at their ends and they only rename one by one rather than merge where their names are the same after running the applet. Many thanks for any help.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
How about:

Code:
$folders = Get-ChildItem -Directory
foreach ($folder in $folders) {
    $newName = $folder.Name -replace "[0-9]", ""
    if (Test-Path -Path (Join-Path $folder.Parent $newName)) {
        Get-ChildItem -Path $folder.FullName | Move-Item -Destination (Join-Path $folder.Parent $newName) -Force
        Remove-Item -Path $folder.FullName -Recurse
    } else {
        Rename-Item -Path $folder.FullName -NewName $newName
    }
}
 
Upvote 0
How about:

Code:
$folders = Get-ChildItem -Directory
foreach ($folder in $folders) {
    $newName = $folder.Name -replace "[0-9]", ""
    if (Test-Path -Path (Join-Path $folder.Parent $newName)) {
        Get-ChildItem -Path $folder.FullName | Move-Item -Destination (Join-Path $folder.Parent $newName) -Force
        Remove-Item -Path $folder.FullName -Recurse
    } else {
        Rename-Item -Path $folder.FullName -NewName $newName
    }
}
Many thanks for this, I think its on the right track but...

I think its calculated the folder I set up but it looks to have removed all the other folders?! They are only copies and so it does not matter, just need to reimport the folders...

...I would like to be able to add multiple functions so say there is a 1 through to 9 on the end but some folders also have letters and words. Can you add a string to manage several items that you ask it to remove rather than just 1 through to 9?...and can you stop it from removing the other folders which I think it has done?
 
Upvote 0
How about:

Code:
$patternsToRemove = "[0-9]", "word1", "word2"  # Add as many as you'd like
$folders = Get-ChildItem -Directory
foreach ($folder in $folders) {
    $newName = $folder.Name
    foreach ($pattern in $patternsToRemove) {
        $newName = $newName -replace $pattern, ""
    }
    $newName = $newName.Trim()
    if (Test-Path -Path (Join-Path $folder.Parent $newName)) {
        Get-ChildItem -Path $folder.FullName | Move-Item -Destination (Join-Path $folder.Parent $newName) -Force
        Remove-Item -Path $folder.FullName -Recurse
    } else {
        Rename-Item -Path $folder.FullName -NewName $newName
    }
}
 
Upvote 0
Many thanks for this, I think its on the right track but...

I think its calculated the folder I set up but it looks to have removed all the other folders?! They are only copies and so it does not matter, just need to reimport the folders...

...I would like to be able to add multiple functions so say there is a 1 through to 9 on the end but some folders also have letters and words. Can you add a string to manage several items that you ask it to remove rather than just 1 through to 9?...and can you stop it from removing the other folders which I think it has done?
Thank you. It looks to have done the same again, merged one folder and removed the other/s whereas I need it to merge all the folders that apply and keep them where they are. I'm guessing its deleting the folders that do not apply?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top