You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 %%a later.
  • 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 on
  • if not errorlevel 1: Checks if findstr found a match (errorlevel 0 = success, errorlevel 1 = no match)

Why your earlier findstr attempt might have failed:

  • You probably forgot the /r flag (without it, findstr treats 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 /i could 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:12:17