Page 1 of 3

year and countryname extract

Posted: Sun Jul 08, 2018 10:45 am
by sshree43
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

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
I am trying to use split Group-Object {$_.Basename.Split(' ')[2]}| but unable to use in above condition

thanks

Re: year and countryname extract

Posted: Sun Jul 08, 2018 11:33 am
by jvierra
More like this:

Code: Select all

Get-ChildItem -File -Path $sourcedir|
    select *, @{n='Year';e={($_.Basename -split '_')[2]}}
    Group-Object Year | 
    ForEach-Object {
      $_.Group | Sort-Object LastWriteTime -Descending |
        Select-Object -First 1
    }

Re: year and countryname extract

Posted: Sun Jul 08, 2018 12:06 pm
by sshree43
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

Code: Select all

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 

Re: year and countryname extract

Posted: Sun Jul 08, 2018 12:12 pm
by jvierra
Works fine for me:

('abc_China_2017_1' -split '_')[2]

Re: year and countryname extract

Posted: Sun Jul 08, 2018 1:33 pm
by sshree43
these both name are same...and 2nd row file should go to archive..which is not happening

Code: Select all

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

Re: year and countryname extract

Posted: Sun Jul 08, 2018 3:04 pm
by jvierra
You wanted the newest file and that is the first file.

Re: year and countryname extract

Posted: Sun Jul 08, 2018 8:36 pm
by sshree43
please help me with move script

Re: year and countryname extract

Posted: Sun Jul 08, 2018 8:41 pm
by jvierra
What is your question? We cannot guess at what you want from what you have posted.

So far you have posted this in various forms in many forums and no one has been able to guess at what you are asking.

Above you posted code that gets the newest file. If you really want the oldest file don't use "descending".

Re: year and countryname extract

Posted: Sun Jul 08, 2018 11:28 pm
by sshree43

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
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

Re: year and countryname extract

Posted: Mon Jul 09, 2018 12:07 am
by jvierra
What does that mean? What have you tried?