I am attempting to batch edit metadata of some video files by piping the results from ls into a function that edits the video file's name in metadata. Example:
Files:
- My Movie 1 - 1080p DTS 5.1.mkv
- My Movie 2 - 720p DTS 2.1.mkv
- My Movie 3 - 480p AAC 1.0.mkv
Desired title renames in metadata:
- My Movie 1
- My Movie 2
- My Movie 3
I am using the mkvpropedit function from MKVToolNix in PowerShell, which can be used to rename a single file's title in the following way:
C:\"Program Files"\MKVToolNix\mkvpropedit 'My Movie 1 - 1080p DTS 5.1.mkv' -e info -s title='My Movie 1'
This works perfectly, however I would like to be able to perform this action for an entire directory, removing everything from the dash on in the metadata title. Here is the code I tried below, piping the results of ls into the command (I should mention that every single file in the directory is a movie file I wish to edit) and capturing the desired portion of the filename with a regex.
ls | C:\"Program Files"\MKVToolNix\mkvpropedit {$_.name} -e info -s title={$_.name -replace('(.+)-.+', '$1')}
The regex is correct as far as I can tell, but I receive the following error:
mkvpropedit.exe : The command parameter was already specified. At line:1 char:6
ls | C:"Program Files"\MKVToolNix\mkvpropedit {$_.name} -e info -s t ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [], ParameterBindingException
FullyQualifiedErrorId : ParameterSpecifiedAlready
As suggested in the comments, I tried a foreach loop as well:
ls | foreach{C:\"Program Files"\MKVToolNix\mkvpropedit {$_.name} -e info -s title={$_.name -replace('(.+)-.+', '$1')}}
This produced the same error, once for each file in the directory. What am I doing wrong here? Is there a better way to approach my desired outcome? Thanks!