Extract episode title from filename for metadata

ultimately, i’d like to get something like the following to work:

`ls -filter (.*\dE\d* )(.*)( 1080p.*)\.mkv | % {mkvpropedit.exe '$_.name' --edit info --set "title=$2" }`

I want to add a title field to a folder of mkv files.
individually, this can be done using:

` mkvpropedit.exe filename --edit info --set "title=episode_title" `

The video files are part of a series with a format:

Series_name SxxExx.Episode_Title.1080p...mkv 

I added the periods in the middle of the filename to make it easier to use split(“.”), but I would prefer to parse the string using

(.*\dE\d* )(.*)( 1080p.*)\.mkv

as shown above.

After adding the periods to the filename, I tried using split:

 ls -filter *.mkv | % {mkvpropedit.exe $_.name --edit info --set "title=$_.name.split('.')[2]" } 

but this just assigned title the entire filename.

Any help pointing me in the right direction would be appreciated.
And, yes, i seem preoccupied with one line commands. lol

Everything after $_ is interpreted as a string since it’s all in quotes. You need to escape the string

"title=$($_.name.split('.')[2])"

Thanks. This is the second time I made the same mistake. haha.
One slight correction is that it seems the array index starts at 0, so I need [1] rather than [2].

Is there any way to parse the filename using

(.*\dE\d* )(.*)( 1080p.*\.mkv)

so I can just use $2 (or point to (.*) ) instead of $_.name.split('.')[1]?
It would just be more elegant to have a solution that didn’t require me to rename the file names twice, meaning adding a period so I can use split(‘.’) and then removing the periods afterwards.

But thanks for pointing out the need for the additional $().

What do the file names look like normally? Why would you feel you need to add a period to split, when you can split on a space or another character?

Also, can you please go back and format your code properly?

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

The structure is normally:

Series_name SxxExx Episode_Title 1080p*

(or whatever resolution)

I added the periods after the SxxExx and before the 1080p so the split would place the entire episode title in one array element, since the episode title varies in length and is always bound between these two identifiers.
Alternatively, I could have files with names where each empty space has been replaced with periods, which is standard for some people. In either instance, the split solution only works if the episode title contains no periods.
And, yes, I will clean up the code. I thought i had used the proper escape characters when I submitted the question, but clearly didn’t and I just haven’t had the time until now.

* is not a valid character for a file. I’ll assume that you mean

Series_name SxxExx Episode_Title 1080p.mkv

which may render my suggestion useless if my assumption is incorrect. But if there is not a space in the title and you can depend on there being only space after it (the one directly after it) then this could be simplified to

Get-ChildItem -filter '*1080p.mkv' | ForEach-Object {
    "mkvpropedit.exe '$_.name' --edit info --set `"title=$(($_.name -split ' ')[-2])`" "
}

Please note that I didn’t use the aliases. They are fine when just typing in the console but for shared/production scripts it is frowned upon to use them. :slight_smile:

1 Like