year and countryname extract

Ask your Windows 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.
User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

year and countryname extract

Post by sshree43 » 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

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

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: year and countryname extract

Post by jvierra » Sun Jul 08, 2018 11:33 am

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
    }

User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Re: year and countryname extract

Post by sshree43 » Sun Jul 08, 2018 12:06 pm

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 

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: year and countryname extract

Post by jvierra » Sun Jul 08, 2018 12:12 pm

Works fine for me:

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

User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Re: year and countryname extract

Post by sshree43 » Sun Jul 08, 2018 1:33 pm

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

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: year and countryname extract

Post by jvierra » Sun Jul 08, 2018 3:04 pm

You wanted the newest file and that is the first file.

User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Re: year and countryname extract

Post by sshree43 » Sun Jul 08, 2018 8:36 pm

please help me with move script

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: year and countryname extract

Post by jvierra » Sun Jul 08, 2018 8:41 pm

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

User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Re: year and countryname extract

Post by sshree43 » Sun Jul 08, 2018 11:28 pm

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

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: year and countryname extract

Post by jvierra » Mon Jul 09, 2018 12:07 am

What does that mean? What have you tried?

Locked