Page 1 of 2

split satement

Posted: Wed Jul 11, 2018 6:11 am
by sshree43
Hi Expert,

I am using split condition to split the space and underscore and I am reaching to country and year but i wanted to stop after that

i tried Group-Object {$_.Basename.Split(' ')[3],('_')[1]} | in this way i can get countryname _year but how to stop after the year and should not go further so that i can make easily group by a statement by country and year

Code: Select all

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

Re: split satement

Posted: Wed Jul 11, 2018 6:20 am
by jvierra
$country = ('abc N Refund Maker_Japan_2017_302413 modified date :26/06 10:20' -split '_')[1]
$year = ('abc N Refund Maker_Japan_2017_302413 modified date :26/06 10:20' -split '_')[2]


If you would take some time to learn PowerShell instead of trying to guess at everything you would find that this is all very easy.

Re: split satement

Posted: Wed Jul 11, 2018 9:29 am
by sshree43
Dear Sir,

My apology for guessing
the same thing i can achieve through Group-Object {$_.Basename.Split(' ')[3],('_')[1]} this script..can we able to split only country and year only without hardcoded

Re: split satement

Posted: Wed Jul 11, 2018 9:53 am
by sshree43
at least tell me about match condition

Where-Object BaseName -match ' ([a-z]+_20\d{2})_\d' |

Re: split satement

Posted: Wed Jul 11, 2018 11:28 am
by sshree43
i got new logic into it

$Match = "[^_]+_[0-9]{4}"
$Lines = get-clipboard
$Results = $Lines | %{$_ -Match $Match | %{$Matches[0]}}

Re: split satement

Posted: Wed Jul 11, 2018 1:44 pm
by sshree43
Hi expert,

Code: Select all

can you help in adding above logic into it

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

Re: split satement

Posted: Wed Jul 11, 2018 2:03 pm
by jvierra
As I have posted more than 5 times, I cannot understand what you want and I cannot write custom code for you. If you would take the time to learn and understand PowerShell and basic programming you would probably be able to ask a better question.

If this is of critical importance then you should hire a consultant to work with you to figure out what you are trying to do. A simple web forum is a very bad place for solving difficult problems.

I know you know in your head what you want. Wanting to something does not make it possible and asking vague and ever-changing questions does not help in finding an answer.

I have now posted 5 or more solutions to your questions in three different forums and each time you have responded by changing the question.

Until you have a clear question there is nothing that any of us can do except guess. I am out of guesses.

Re: split satement

Posted: Wed Jul 11, 2018 2:18 pm
by sshree43
very simple question needs to extract year and country from file name and keep their last modified date files and move remaining files to archive...simple English

Re: split satement

Posted: Wed Jul 11, 2018 2:31 pm
by jvierra
Simple to say but no examples that any of us have given you have been acceptable. Answering that question always leads to a new statement of the question with new rules. An you understand my point?

Re: split satement

Posted: Wed Jul 11, 2018 10:44 pm
by sshree43
can you help in adding logic into it...believe me it is final

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 ' ([a-z]+_20\d{2})_\d' |Group-Object {$Matches[1]} |

ForEach-Object {$_.Group | Sort-Object LastWriteTime -Descending |

Select-Object -Skip 1 |

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