split satement

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

split satement

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

Re: split satement

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

Re: split satement

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

Re: split satement

Post by sshree43 »

at least tell me about match condition

Where-Object BaseName -match ' ([a-z]+_20\d{2})_\d' |
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: split satement

Post by sshree43 »

i got new logic into it

$Match = "[^_]+_[0-9]{4}"
$Lines = get-clipboard
$Results = $Lines | %{$_ -Match $Match | %{$Matches[0]}}
User avatar
sshree43
Posts: 33
Last visit: Wed Jul 18, 2018 8:53 am

Re: split satement

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

Re: split satement

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

Re: split satement

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

Re: split satement

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

Re: split satement

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