I have a long list of files where various clerks named them inconsistently. They will all have names that begin with a series of numbers representing a 'date' followed by a description. Example
12-16-21 Report.pdf
2-12-22-Report.pdf
03-15-22_report.pdf
I'm looking for a Powershell script or BAT file that can parse these into a consistent format. At least the -date- portion so they sort properly. I don't care about the follow on descriptive text.
2021-12-16 Report.pdf
2022-02-12-Report.pdf
2022-03-15_report.pdf
I tried a Powershell script to rename based on Last Modified Date and found that those dates are not reliable. So I need help to see if there is a regular expression that can be used to parse out the initial numeric chars, reformat as yyyy-MM-dd-whatever, and rename.
$ParsedDate = [datetime]::MinValue;
$Path = "I:\Test";
Get-ChildItem -File -Path $Path -Recurse |
Where-Object { (-not [regex]::IsMatch($_.Name, "^\d{8}_", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)) -or (-not [datetime]::TryParseExact($_.Name.Substring(0, 8), "yyyyMMdd", [cultureinfo]::CurrentCulture, [System.Globalization.DateTimeStyles]::None, [ref] $ParsedDate)) } |
ForEach-Object { Rename-Item -Path ($_.FullName) -NewName "$($_.LastWriteTime.ToString("yyyyMMdd"))_$($_.Name)"; }
So... can someone provide some help with parsing the file name based on those initial numeric chars?