I have a folder with list of all of the movies inside this folder in following format.

  • [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 would like to get all of the movies in that folder and go through them and put them into three different attribute Year, Name, Studio

All this info will be save into a single file name of the movie

This is how I wont to save the info into the file

Name of the movie [P1]


Name of the movie [P1][Director Edition]

I try to write this code

$regex = [regex]"\[(\w+)\](\w+\[\w+\])-(\w+)"
$tokens = $regex.Match($name).groups[1,2,3] | Select -ExpandProperty Value

Which worked fine but when running it like this but it doesnt work when I run it in loop.

$name = dir *.mp4 | select BaseName
$regex = [regex]"\[(\w+)\](\w+\[\w+\])-(\w+)"
foreach ($n in $name)
    $file_name = $n.BaseName.ToString();
    $year, $title, $studio = $regex.Match($file_name).groups[1,2,3] | Select -ExpandProperty Value
  • 1
    The question is confusing, in part by formatting and grammar. Could you please clarify what the current name format is and what the desired format is? You also provided some code, which is great, but fail to mention where your code encounters problems. $name.IndexOf("[") (and "]") will select the index in your string of the first occurrence of brackets starting from the left. – root Feb 28 '18 at 14:07
  • I have updated the question to make it more clear. – maj Mar 1 '18 at 11:47
  • Unlike e.g. [1979] Name of the movie [P1] - Disney, your test case $name = "[TEST]TEST[TEST]-TEST" does not contain spaces… Update the regex… – JosefZ Mar 1 '18 at 20:39
  • how do i add that to make sure if the name has space it will work. – maj Mar 4 '18 at 18:10

I'm not a regex expert, sorry; however, I can offer my rudimentary attempt:

$name = dir *.mp4 | select BaseName    ### no such files; see next herestrig workaround:
$name = @'
[1979] Name of the movie [P1] - Disney
[1979] Name of the movie [P1]  [Director's Cut] - PTC
[1980] Name of the movie [P8][Director Edition] - Test Studios
'@ -split [System.Environment]::NewLine

$regex = [regex]"\[(\w+)\](\w+\[\w+\])-(\w+)"                          # wrong: original
$regex = [regex]"\[(\w+)\]([\s*\w]+[\s*\[\w+\]]+)\s*-\s*(\w+)"         # wrong: Apostrophe
$regex = [regex]"\[(\w+)\]\s*(\w+[\s*\[\w+'*\]]+?)\s*\-\s*([\s*\w+]+)" # works
$regex = [regex]"\[(\w+)\]\s*(\w+[\s*\[\w+'*\]]+?)\s*\-\s*(.*$)"       # works

foreach ($n in $name)
    $file_name = $n #.BaseName.ToString();
    $year, $title, $studio = $regex.Match($file_name).groups[1,2,3] |
        Select -ExpandProperty Value
    "$year,$title,$studio,"     ### debugging output


PS D:\PShell> D:\PShell\SU\1298893.ps1
1979,Name of the movie [P1],Disney,
1979,Name of the movie [P1]  [Director's Cut],PTC,
1980,Name of the movie [P8][Director Edition],Test Studios,

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.