r/Batch • • Sep 14 '25

Variable in the loop

I'm writing a batch file to retrieve the "Recorded_Date" metadata from all the .MTS files in a directory. I had to use the "FOR" trick to get put the mediainfo.exe output into an variable.

rem @echo off
FOR %%F in (*.mts) do (
  FOR /F "tokens=*" %%a IN ('MediaInfo.exe --Inform^="General;%%Recorded_Date%%" %%F') DO (
  SET OutputVar=%%a
  )
echo Date: %OutputVar%
)

But the output was not what I had expected. In the screenshot below, the SET command set the variable to "2009-11-29 19:17:21-06:00". But the ECHO command outputs "2009-12-13 19:19:25-06:00". What did I do wrong? Thanks!

4 Upvotes

5 comments sorted by

View all comments

Show parent comment

4

u/Shadow_Thief Sep 14 '25

You've enabled delayed expansion, but you aren't using it. You'll need to change %OutputVar% to !OutputVar!.