Search code examples
powershellwhitespacebatch-renameleading-zero

Batch rename - remove parentheses and whitespace and have leading zero


so I know there are like a million similar questions (but sadly not quite the same, as always), but since I'm fairly new to Powershell and have real trouble with its syntax I hope someone is willing to help me out

I already prepared the files I have with the explorer batch rename:

Name
----
#20AR (1).doc
#20AR (2).doc
...
#20AR (10).doc
#20AR (11).doc

Now, all I want is for the filenames to look like this:

#20AR01.doc
...
#20AR10.doc

So I just want to have leading zeros, for the one digit numbers, and get rid of the parenthesis and the white space (the syntax provided in one SO answer sadly didn't work for me).


Solution

  • What you're trying to do is fairly trivial:

    Get-ChildItem '#20AR*.doc' | Rename-Item -NewName { $_.Name -replace ' \((\d)\)','0$1' -replace ' \((\d\d)\)','$1' }