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