year and countryname extract

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 8 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.
Locked
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

year and countryname extract

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: year and countryname extract

Post 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
    }
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: year and countryname extract

Post 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 
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: year and countryname extract

Post by jvierra »

Works fine for me:

('abc_China_2017_1' -split '_')[2]
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: year and countryname extract

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: year and countryname extract

Post by jvierra »

You wanted the newest file and that is the first file.
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: year and countryname extract

Post by sshree43 »

please help me with move script
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: year and countryname extract

Post 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".
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: year and countryname extract

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: year and countryname extract

Post by jvierra »

What does that mean? What have you tried?
This topic is 5 years and 8 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.
Locked