split satement

Ask your Windows 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.
User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

split satement

Post by sshree43 » Wed Jul 11, 2018 6:11 am

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

User avatar
jvierra
Posts: 12398
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: split satement

Post by jvierra » Wed Jul 11, 2018 6:20 am

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

Re: split satement

Post by sshree43 » Wed Jul 11, 2018 9:29 am

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

Re: split satement

Post by sshree43 » Wed Jul 11, 2018 9:53 am

at least tell me about match condition

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

User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Re: split satement

Post by sshree43 » Wed Jul 11, 2018 11:28 am

i got new logic into it

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

User avatar
sshree43
Posts: 33
Joined: Wed Jun 27, 2018 11:57 am

Re: split satement

Post by sshree43 » Wed Jul 11, 2018 1:44 pm

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 }

User avatar
jvierra
Posts: 12398
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: split satement

Post by jvierra » Wed Jul 11, 2018 2:03 pm

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

Re: split satement

Post by sshree43 » Wed Jul 11, 2018 2:18 pm

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

User avatar
jvierra
Posts: 12398
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: split satement

Post by jvierra » Wed Jul 11, 2018 2:31 pm

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

Re: split satement

Post by sshree43 » Wed Jul 11, 2018 10:44 pm

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

Post Reply