Removing a date from a Filename with Powershell

Solved

Edit:

I figured it out

-replace '__\d{6,8}'

This will match any date after __ between 6 to 8 digits. (caters for dates that do not have 01 for both months and days so matches 201811 and matches 20180101)

If there is any better way of doing this let me know?


Original Post:

Hello, I am struggling to work out how to regex this, but how can i remove __20181216 from a file name and still maintain the extension.

Here is a list of files, and I am trying to remove the date from all of them.

CSSWholeFoodsStore_SS9__20181216.rar
CSSGreenTreeLand_SS9__20181216.rar
CSSWindows10_SS12__20181216.rar
_ServerPerfMonitor_SS11__20181216.rar
CSSSportsStore_SS12__20181216.rar  

Also I am not sure if future files the date will contain 01 or just 1. So really I am looking to remove anything from double underscore onwards until .rar

my perfect file name would be

CSSSportsStore_SS12.rar

Thanks guys for your help, really appreciate it.

*names have been replaced, but the format is correct.

Posted Dec 16, 2018, 11:40 PM UTC · edited

1314
Loading comments…