Could this be another opportunity for ConvertFrom-String??
$template = @'
[{Year*:1979}] {Name:Name of the movie [P1]} - {Studio:Disney}
[{Year*:1979}] {Name:Name of the movie [P1][Director Edition]} - {Studio:PTC}
[{Year*:1980}] {Name:Name of the movie [P8][Director Edition]} - {Studio:Test}
'@
$testText = @'
[1979] Name of the movie [P1] - Disney
[1979] Name of the movie [P1][Director Edition] - PTC
[1980] Name of the movie [P8][Director Edition] - Test
'@
PS C:\Users\me> $testText | ConvertFrom-String -TemplateContent $template
Year Name Studio
---- ---- ------
1979 Name of the movie [P1] Disney
1979 Name of the movie [P1][Director Edition] PTC
1980 Name of the movie [P8][Director Edition] Test
I have folder with all of the movies. Which i like to split in three parts.
This is how Text file should look like
Year: 1979
Name: Name of the movie [P1]
Studio: Disney
Any thing in first will be 1st part of the file, any thing after this until - will be 2nd part of the file and anything after - will be 3rd part of the file.
I just try my code again and its work but because I have space in my movies name it doesnt work. i think I need to make changes to $regex = [regex]“[(\w+)](\w+[\w+])-(\w+)”
I spend a little effort to try to help you. So could you please at least try the code I posted and tell me if it works? And if it does not work, tell what does not work.
I did try your code which works on some movies but not all.
lets say I have movie which is name in this way [1979] Name of the movie [P1][Director Edition] [Somthing here] 1979] Name of the movie [P1][Director Edition] then something more – PTC
What your code does is that it puts each brackets into new array which i dont wont, I just wont to split the name into three values
like this
1979
Name of the movie [P1][Director Edition] [Somthing here] 1979] Name of the movie [P1][Director Edition] then something more
PTC
hmmm … you gave three example file names and I’m almost sure about that - for these three example file names my code worked as intended, right?
String manipulation is always error prone if you don’t have enough information about the possible patterns in the source strings.
Actually I wanted to give you some extra goodies and split your file names in (for me) logical pieces like a database would have. Each single piece of information is stored in single cell. But therefore the source data would have to have a certain structure as well.
The new file name pattern you gave looks weird for me. Just like something went wrong in an earlier naming step of some other script.
But anyway, I don’t like to give up. So here is another try for the example file names you gave:
BTW: I had some problems recognizing what dash, minus, hyphen or whatever you use in your file names. So I included 4 of them in the pattern and hope one of them fits. If not - you have to do some research for it.
This code works? Really? If I try it with the example file names you posted it does not work at all. I get a lot of errors.
And regardless of that - do you ever answer questions? :-/
Because you are copying file name from this site it add -(Dash) which code doesnt like.
PS C:\Users\User> $Path = 'D:\Training\Test'
$regex = "\[([\w|\s]+)\]([\w|\s]+\[[\w|\s]+\].*)\s*-\s*([\w|,|\s]+)"
Get-ChildItem -Path $Path -Filter *.txt |
ForEach-Object {
$name = $_.BaseName -replace '–', '-'
$groups = [regex]::Match($name, $regex).Captures.Groups
$groups[1].Value
$groups[2].Value.Trim()
$groups[3].Value
Write-Host
}
1979
Name of the movie [P1]
Disney
1979
Name of the movie [P1][Director Edition]
PTC
1980
Name of the movie [P8][Director Edition]
Test