I’m trying to rename some csv files using Regex and I’m getting incorrect output. I’m losing the file extension and the variable I’m trying to include as a prefix in the regex is going in as a literal. Intended results, actual results, and code below:
Incoming files
d:files9001.001 (2).csv
d:files9001.004.csv
d:files9002.006 (3).csv
d:files9002.007 (2).csv
d:files9002.008 (2).csv
Desired output
d:filesDB17-9001-001.csv
d:filesDB17-9001-004.csv
d:filesDB17-9002-006.csv
d:filesDB17-9002-007.csv
d:filesDB17-9002-008.csv
Actual Output
d:files$prefix-9001-001
d:files$prefix-9001-004
d:files$prefix-9002-006
d:files$prefix-9002-007
d:files$prefix-9002-008
Code
$folder_input="d:files" $prefix="DB17" Get-ChildItem $folder_input*.csv| Foreach-Object { Rename-Item $_ ($_.BaseName -replace "^.*?([0-9]{4}).([0-9]{3})[ ].*" ,'$prefix-$1-$2') }
submitted by /u/JeffIpsaLoquitor
[link] [comments]
The post File renaming with regex issues appeared first on How to Code .NET.