I have a lot of half-hourly files with names in the form A_B_C_YYYYMMDDThhmm_YYYYMMDDThhnn.csv
A, B and C are words
where YYYY
is the year in 4 digits,
MM
the month in 2 digits,
DD
the day of the month in 2 digits,
hh
the hour of the day in 2 dgits,
mm
the minute in 2 digits,
and nn
is mm+30 minutes
How can I move these files into folders based on years and months using Powershell?
I have made an attempt based on a previously used script but it is not producing any output for my filename format, presumably because the match
string is wrong:
Push-Location 'path/to/files'
Get-ChildItem _*_*.csv |
Where-Object {$_.BaseName -match '_(\d{4})(\d{2})T(\d{4})_\d{6}T(\d{4})'} | ForEach-Object {
$TargetDir = "{0}\{1}" -f $Matches[1],$Matches[2]
if (!(Test-Path $TargetDir)){MD $TargetDir | Out-Null}
$_ | Move -Destination $TargetDir
}