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

Code: Select all

Get-ChildItem $source -FIle |
    Select-Object *, @{n='year';e={($_.Basename -split '_')[2]}},@{n='Country';e={($_.Basename -split '_')[1]}}
    Group-Object Year,Country |
    Select-Object -expand Group |
    Sort-Object LastWriteTime |
    Select-Object -First 1 |
    Move-Item -Destination $destination
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: year and countryname extract

Post by sshree43 »

when i run this file...only half of the files are moving

$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 ' ([a-z]+_20\d{2})_\d' |
Group-Object {$Matches[1]} |
ForEach-Object {
$_.Group | Sort-Object LastWriteTime -Descending |
Select-Object -Skip 1 |
Move-Item -Destination $destdir -Force
}

i think it is unable to find year and country and only year can get results
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: year and countryname extract

Post by sshree43 »

any suggestions on this i am little bit away from the target as able to move some of the files. here is the new file format first row should be archived

abc N Refund Maker_Japan_2017_302413 modified date :26/06 10:20
abc N Refund Maker_Japan_2017_Extra modified date:26/06 10:30
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