0

Lets say I want to create a batch file. I work with video files and I use this well known tool MediaInfo. And it has a CLI version on Windows.

Lets say I want to rename bunch of files in a folder. So I run this:

for /r %%i in (*.mkv) do call ren "%%i" "renamed_files.mkv"

So what it does is that it picks up all .mkv files in the dir and renames it. That's great.

But what if I want to rename only specific .mkv files and not all of them? E.g. I have multiple .mkv files that has audio and some that doesn't. And lets say I want to rename only those that have audio. So to do that, I need to use some solution to scan/detect which files has audio. And to do that I thought why not use MediaInfo CLI. So we run this command:

mediainfo --inform="Audio;%ID%" file.mkv

And all it does, it prints 2 which is the ID of the audio track, which we could use to confirm that audio exists and the .mkv file should be picked up for renaming. And if there is no audio, it prints nothing, empty line, so those files should be skipped.

Makes sense and all, but I can't figure out how to wrap it up in if statements and make it work since its all new to me, I've been learning command prompt and batch files for 2 days now.

Or maybe someone knows even better solution. Except I'm not interested on installing 3rd party software with GUIs for that.

Cheers!

user3108268
  • 1,043
  • 3
  • 18
  • 37

1 Answers1

0

try

for /r %%i in (*.mkv) do (
  for /f "usebackq" in %%a (´mediainfo --inform="Audio;%ID%" file.mkv´) do set "audio=%%a"
  if defined audio (
    call ren "%%i" "renamed_files.mkv"
  )
)

the second for loop executes the command in parens and sets the audio variable to whatever the command returns. if it returns nothing, then the audio variable will be undefined and, in this case, the if block won't be executed.

elzooilogico
  • 1,659
  • 2
  • 17
  • 18
  • 1
    As in OPs question the call is superfluous. You should initialize audio to prevent using remnants from previous iterations. –  Mar 18 '17 at 10:41
  • Why are you setting `%audio%`, just use `if not "%%a"=="" ren "%%i…`. In addition I'm not sure that renaming each and every file containing audio to the same name is a particularly good idea, nor does there seem any logic in using `call`, _(I know the OP did this too)._ – Compo Mar 18 '17 at 10:44
  • hmm I don't what's wrong but .bat won't even run, it just blinks. – user3108268 Mar 18 '17 at 11:19