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.
jvierra
Posts: 14444
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Wed Jun 27, 2018 11:57 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
Joined: Wed Jun 27, 2018 11:57 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

Locked