powershell recursive rename with wildcard

I have these backup files:
Social_DB_backup_2018_01_07_071010_0794131.BAK
SharePoint_Config_backup_2018_01_07_071010_0326524.bak
BDC_Service_DB_backup_2018_01_07_071010_0482393.bak
 And so on....
I want to rename them
Social_DB_backup.bak
SharePoint_Config_backup.bak
BDC_Service_DB.bak
how can I go recursively through file directory and rename all of the backups?
Thanks!
dkochenoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Depends. In the first two examples, you kept the "_backup" part and only removed the dates, in the BDC example, you removed the _backup as well ...

Both of the following are in test mode and will only show which files they would rename how; remove the -WhatIf argument to run them for real:
To keep the "_backup":
Get-ChildItem -Filter *.bak -Recurse |
	Where-Object {$_.BaseName -match '(?<NewName>.*)_\d{4}_\d{2}_\d{2}_\d{6}_\d{7}'} |
	ForEach-Object {Rename-Item -Path $_.FullName -NewName "$($Matches['NewName'])$($_.Extension)" -WhatIf}

Open in new window


To remove the "_backup" as well:
Get-ChildItem -Filter *.bak -Recurse |
	Where-Object {$_.BaseName -match '(?<NewName>.*)_backup_\d{4}_\d{2}_\d{2}_\d{6}_\d{7}'} |
	ForEach-Object {Rename-Item -Path $_.FullName -NewName "$($Matches['NewName'])$($_.Extension)" -WhatIf}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dustin SaundersDirector of OperationsCommented:
Critera = only remove numbers from end?  

What should happen if there are more than 1 file that would rename to the same?
0
Dustin SaundersDirector of OperationsCommented:
No feedback from Author, but code provided sufficient.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.