typos
typos

Reputation: 6642

How to bulk rename files in PowerShell?

I have a folder with 100+ files, and all of them have a common format, namely one string, followed by 4-digit number specifying year, and then at the end just a single lowercase letter. For example, one valid file would be Test2012b

Now, I want to rename them in such a way that the last year and letter part come to beginning inside the brackets, and then the string at the beginning go to end. My above example filename Test2012b would be renamed to [2012b]Test

I though about just using Rename-Item of PowerShell as below, but I really don't know how can I make the above filename transformation. Any ideas?

Get-ChildItem *.pdf| Rename-Item -NewName { $_.Name -replace ???? }

Upvotes: 0

Views: 490

Answers (2)

Esperento57
Esperento57

Reputation: 17472

i prefer @t1meless solution but it's an other solution

gci *.pdf| rni -NewName {"[{0}]{1}.pdf" -f $_.BaseName.Substring($_.BaseName.Length -5, 5), $_.BaseName.Substring(0, $_.BaseName.Length -5)}

Upvotes: 0

t1meless
t1meless

Reputation: 539

Get-ChildItem *.pdf | Rename-Item -NewName { $_.Name -replace "(\w+?)(\d+)(\w)", '[$2$3]$1' }

Upvotes: 4

Related Questions