How to batch rename part of the filename in Windows PowerShell using Regex?

In this example, we have 6 files below with the date and filename below:

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/06/2018     12:40              0 1 = 9.8.16 Bank Statement 298.67.pdf
-a----       10/06/2018     12:40              0 2 = 9.9.16 Bank Statement 134.74.pdf
-a----       10/06/2018     12:41              0 3 = 9.10.16 Bank Statement 123.43.pdf
-a----       10/06/2018     12:41              0 4 = 9.11.16 Bank Statement 134.54.pdf
-a----       10/06/2018     12:41              0 5 = 9.12.16 Bank Statement 157.98.pdf
-a----       10/06/2018     12:41              0 6 = 9.1.17 Bank Statement 123.45.pdf

Here, the filename contains a serial # at the beginning, followed by =, followed by ‘Bank Statement’ followed by the end of statement balance.

These files are in fact Credit Card Statements and the word ‘Bank’ needs to be replaced by the words ‘Credit Card’. All the rest of the filename should remain the same.

In order to achieve this, we will be using Windows PowerShell with Regex.

To list all the bank statements, we use the ls command, then pipe the output

ls | ren -newname {$_.name -replace('(.+)Bank(.+)','$1Credit Card$2')}

The rename command is used after the pipe to rename the files. $_ is the object that holds the output before the pipe, and we are using the name property of the $_ object. -replace is used on strings in PowerShell, which replaces a string with a new string. -replace is a function that takes 2 parameters. The 1st parameter is the old string that we want to replace and the 2nd parameter is the new string.

Capturing groups in Regex allows us keep parts of the string. In Regex, anything put in parenthesis is captured to use later. eg. (.+)Bank(.+) captures anything before and after the characters ‘Bank’. We can use these captured parts, by using $ and a number. The 1st parenthesis captures into $1 and the 2nd parenthesis captures into $2 etc…

In the 1st parameter, we specify the old string that we want to replace, and at the same time we capture the parts of the string that we want to keep (capture). In the 2nd parameter, the new string uses the captured parts that we want to keep, as well as the new string itself eg. '$1Credit Card$2'.

Result:

Mode       LastWrite      Time            Length   Name
----       ---------      ----            ------   ----
-a----     10/06/2018     12:40              0 1 = 9.8.16 Credit Card Statement 298.67.pdf
-a----     10/06/2018     12:40              0 2 = 9.9.16 Credit Card Statement 134.74.pdf
-a----     10/06/2018     12:41              0 3 = 9.10.16 Credit Card Statement 123.43.pdf -a----     10/06/2018     12:41              0 4 = 9.11.16 Credit Card Statement 134.54.pdf -a----     10/06/2018     12:41              0 5 = 9.12.16 Credit Card Statement 157.98.pdf -a----     10/06/2018     12:41              0 6 = 9.1.17 Credit Card Statement 123.45.pdf

Leave a Reply