year and countryname extract
Posted: Sun Jul 08, 2018 10:45 am
Hi Expert,
I am trying split the country and year from the file name order by LastWriteTime and wants to move remaining file to archive folder by year wise but can only filter the year not and not country name
I am trying to use split Group-Object {$_.Basename.Split(' ')[2]}| but unable to use in above condition
thanks
I am trying split the country and year from the file name order by LastWriteTime and wants to move remaining file to archive folder by year wise but can only filter the year not and not country name
Code: Select all
$sourcedir = 'C:\Users\garang\Documents\input_files\Advisory_rate'
$destdir = 'C:\Users\garang\Documents\input_files\Advisory_rate\Archive'
Get-ChildItem -File -Path $sourcedir|
Where-Object BaseName -match '_(20\d{2})_\d' |
Group-Object $Matches[1] | ForEach-Object {
$_.Group | Sort-Object LastWriteTime -Descending |
Select-Object -Skip 1 | Move-Item -Destination $destdir -Force
}
output is
Mode Last Modified date Length Filename
-a--- 6/25/2018 12:08 AM 31744 abc_Italy_2016_2 - Copy - Copy.xls
-a--- 6/25/2018 12:07 AM 31744 abc_Italy_2016_2 - Copy.xls
-a--- 6/25/2018 12:06 AM 31744 abc_China_2017_1.xls
-a--- 6/25/2018 12:07 AM 31744 abc_NL_2017_2.xls
-a--- 6/25/2018 12:05 AM 31744 abc_Finland_2017_3.xls
in above date row 2 will go to archive because last write time of Italy for the year 2016 is 12:08
thanks