3

I’m having trouble extracting some data from a filename as it seems to be adding a space to the end of the extracted name even if I try using the .trim() trick to remove it doesn’t work. I have also tried counting the length of the filename -1 and it leaves the space but removed the last character instead.. this is making it very difficult for me to direct a path to a folder created as it’s I putting the space into the path..

File names below which I’m trying to extract data from

12 Monkeys S02E10 - Fatherland.txt Colony S02E01 - Eleven Thirteen.txt Prison Break S05E05 - Contingency.txt 

I’m trying to extract the tv show name and create a folder in a new directory then move the file into the folder created..

Here is the code I am using..

$TRANSFER = 'C:\Users\BRACEGIRDLE\Favorites\Desktop\TRANSFER\' $TVSHOWS = 'C:\Users\BRACEGIRDLE\Favorites\Desktop\TV_SHOW\' $pattern = ‘\s+\S[0-9][0-9].*’ Get-ChildItem "$TRANSFER/*.txt" | ForEach-Object{ $target = $_.BaseName -split $pattern Write-Host $target@123 $jon = $TVSHOWS+$target If( -not (test-path $jon)) { New-Item -ItemType Directory -force -Path $jon } Copy-Item -path $_.FullName -Destination $jon } 

And here is the error

Quantico @123 Copy-Item : Could not find a part of the path 'C:\Users\BRACEGIRDLE\Favorites\Desktop\TV_SHOW\Quantico \Quantico S02E10 - JMPALM.txt'. At line:20 char:9 + Copy-Item -path $_.FullName -Destination $jon + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Copy-Item], DirectoryNotFoundExcept ion + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerSh ell.Commands.CopyItemCommand 

As you can see I have for this illustration put the variable (tv show name) into a ‘sentence’ so you can see the space added to it.. I have tried adding \s+ which removes one of the spaces but I can’t get rid of th other one regardless of using the trim trick or not..

Can someone help, thanks Connor Bracegirdle

0

2 Answers 2

1

Just to show a proper Regular Expression is very well capable of
getting not only the name but all elements contained in the file name
by using capture groups enclosing the parts in parentheses

**EDIT see the RegEX live on https://regex101.com/r/Vbhq7D/1 **

## Q:\Test\2018\06\10\SU_1330038.ps1 $TRANSFER = 'C:\Users\BRACEGIRDLE\Favorites\Desktop\TRANSFER\' $TVSHOWS = 'C:\Users\BRACEGIRDLE\Favorites\Desktop\TV_SHOW\' $Pattern = '(.*)\s+S(\d{2})E(\d{2})[\- ]+(.*)' Get-ChildItem $TVSHOWS *.txt| Where-Object BaseName -match $Pattern | ForEach-Object{ $jon = Join-Path $TVSHOWS $Matches[1] If( -not (Test-Path $jon)) { New-Item -ItemType Directory -Force -Path $jon |Out-Null } $_ | Copy-Item -Destination $jon [pscustomobject]@{ Name = $Matches[1] Series = $Matches[2] Episode= $Matches[3] Title = $Matches[4] } } 

The script does the sub dir creation, the copy and also shows this output:

Name Series Episode Title ---- ------ ------- ----- 12 Monkeys 02 10 Fatherland Colony 02 01 Eleven Thirteen Prison Break 05 05 Contingency 

Sample tree on my Ramdrive a:

> tree A: /F A:\ │ 12 Monkeys S02E10 - Fatherland.txt │ Colony S02E01 - Eleven Thirteen.txt │ Prison Break S05E05 - Contingency.txt │ ├───12 Monkeys │ 12 Monkeys S02E10 - Fatherland.txt │ ├───Colony │ Colony S02E01 - Eleven Thirteen.txt │ └───Prison Break Prison Break S05E05 - Contingency.txt 
3
  • Thanks LotPings, I was settling for tv show with all episodes from seasons in 1 folder to make it easier for me to write a script seen as I’m new to powershell, this gives me a good insight on how I can do the folders for seasons as well.. just a question.. how does the matches{1-4} relate to the $pattern..? How does it identify each one as an individual category? Commented Jun 10, 2018 at 14:50
  • Sorry the §$%&! Android app didn't let post a comment. I have included a link to regex101.com where the Regular Expression is detailed in the upper right corner. Counted from left to right each pair of parentheses (capture groups) in the pattern is stored in the collection $Matches[1..4]. Commented Jun 10, 2018 at 16:00
  • Thanks LotPings, the example you have gave me with the regex website has made it very clear how to use regex as well before implementing it into my scripts. I have managed to implement seasons into this code as well.. thanks much appreciated Commented Jun 12, 2018 at 6:03
3

Check the result of -split. It creates an array of 2 members

10JUN2018:012955 /:> $txts = Get-ChildItem "$TRANSFER/*.txt" 10JUN2018:012955 /:> ($txts[0].basename -split $pattern).GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String[] System.Array 10JUN2018:012955 /:>"some random stuff" -split " stuff" some random 10JUN2018:013239 /:>("some random stuff" -split " stuff")[0] some random 

The modified code with tweak:

$TRANSFER = "E:\Code\PS\myPS\2018\Jun\10" $TVSHOWS = "E:\code\PS\myPS\2018\Jun\TV_SHOW\" $pattern = ‘\s+\S[0-9][0-9].*’ Get-ChildItem "$TRANSFER/*.txt"| ForEach-Object { $target = ($_.BaseName -split $pattern)[0] Write-Host "$target@123" $jon = $TVSHOWS+$target } 

Output:

12 Monkeys@123 Colony@123 Prison Break@123 
1
  • Thanks Ketanbhut! Been trying to solve this for a few days now.. I did try the [0] at the end of the split but didn’t think to try the brackets! Something so simple, thanks for explaining it as well, much apprciated Commented Jun 10, 2018 at 10:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.