如何在Windows批处理中用正则匹配mkvmerge输出的章节与字幕?
Solution to Detect MKV Files with Chapters and/or Subtitles
Got it, let's tweak your batch script to detect either chapters, subtitles, or both using findstr's regex mode. The main issue with your earlier findstr attempt was likely incorrect regex syntax or missing flags—here's a working version:
@echo off set "rootfolder=Z:\Movies" for /r "%rootfolder%" %%a in (*.mkv) do ( # Check if mkvmerge output contains "chapters" OR "subtitles" mkvmerge -i "%%a" | findstr /r /i /m "chapters\|subtitles" >nul if not errorlevel 1 ( echo "%%a" has chapters and/or subtitles - executing your interesting stuff! # Add your actual "interesting stuff" commands here ) else ( echo "%%a" has neither chapters nor subtitles ) )
Breakdown of the key changes:
set "rootfolder=Z:\Movies": Removed inner quotes from the variable definition to avoid nested quote issues when referencing%%alater.findstr /r /i /m "chapters\|subtitles":/r: Enables regular expression mode (required for the OR logic)/i: Makes the match case-insensitive (catches variations like "Chapters" or "SUBTITLES")/m: Stops searching as soon as a match is found (speeds up processing for large files)"chapters\|subtitles": The regex pattern—\|acts as the OR operator, matching either keyword
>nul: Suppresses printed matched lines; we only care if a match exists, not which line it was onif not errorlevel 1: Checks iffindstrfound a match (errorlevel 0= success,errorlevel 1= no match)
Why your earlier findstr attempt might have failed:
- You probably forgot the
/rflag (without it,findstrtreats the pattern as a literal string instead of regex) - In batch, the regex OR operator needs to be escaped as
\|(a plain|would be interpreted as a pipe command) - Missing
/icould have caused case-sensitive matches to miss lines with capitalized keywords
This script will now trigger your "interesting stuff" for any MKV file that has chapters, subtitles, or both—exactly what you need!
内容的提问来源于stack exchange,提问作者Karsten Jensen
相关产品推荐
相关产品推荐

