Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
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
$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
I am trying to use split Group-Object {$_.Basename.Split(' ')[2]}| but unable to use in above condition
small correction,
After first word of the file name does not contain '_' so difficult to find country and year order by and it is not select statement remaining file has to go to archive folder $dest. in the below case 2 nd row file should go to archive
Select-Object -Skip 1 | Move-Item -Destination $destdir -Force this statement moving the files to Archive for which year and max last modified date and I just wanted to include country name into it which unable to do it
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.