0

I found a batch file script to do the job I need to do. I posed the question of how to add directories and subdirectories recursively in the batch process in a comment on the post I got this code from, but no one replied after 4 months. I couldn't find an answer to this question searching this site so I figured id start a new thread on the topic.

What I wanted to do was be able to bulk change meta data information in MKV files which was accomplished thusly.

#!/bin/bash

# This script takes all mkv files in the current directory and sets the filename
# (without .mkv) as its title in metadata

for mkvfile in *.mkv; do
mkvpropedit "$mkvfile" -e info -s title="${mkvfile::-4}"
done

How can I modify this to be run on a root directory to include all folders and sub-folders in the process? Thank you!

1 Answer 1

2

With bash, you'd set the globstar and nullglob shell options and use ** to recurse into subdirectories:

shopt -s globstar nullglob
for mkvfile in **/*.mkv; do
    mkvpropedit "$mkvfile" -e info -s title="${mkvfile::-4}"
done

See 4.3.2 The Shopt Builtin in the manual.

Note you can also use ${mkvfile%.mkv} to chop off the extension.

You can also use find, but I think it gets pretty ugly

find . -name '*.mkv' -exec sh -c 'mkvpropedit "$1" -e info -s title="$(basename "${1%.mkv}")"' sh {} \;
16
  • Getting error shopt: not found, Bad substitution May 10, 2018 at 19:33
  • Are you using /bin/bash or /bin/sh ? shopt is bash-specific. May 10, 2018 at 19:34
  • /bin/bash - same as the code sample I provided. May 10, 2018 at 19:35
  • find . -name '*.mkv' -exec sh -c 'mkvpropedit "$1" -e info -s title="${1%.mkv}"' sh {} \; Works like a charm but it puts the directory path in the metadata name also. May 10, 2018 at 19:37
  • Silly me I had a typo in the new script. Apologies, your script works perfectly. No error after the correction. ;) May 10, 2018 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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