admin管理员组文章数量:1312895
I have this batch which works as expected giving the video height using mediainfo
@echo off
setlocal enableextensions enabledelayedexpansion
REM set "HEIGTH=1081"
for /f "tokens=* delims=" %%j in ('
"mediainfo --Inform=Video;%%Height%% "b:\VID\myVideo.avi""
') do set "HEI=%%j"
echo=!HEI!
Other working way in line 5: mediainfo --Inform^="Video;%%Height%%" "b:\VID\myVideo.avi"
If HEIGHT is already defined as a variable (remove REM to enable it), result will change to HEIGHT content instead (1081 in this example). Couldn't find a working way to avoid %var% replacing and at the same time getting mediainfo to work as expected (it gives all info or nothing, instead of expected Height property of given video). Already read about %% escape and other answers, but couldn't solve this problem. Only workaround is to previously blank HEIGHT var to make sure command will work and avoid all those var names of mediainfo properties used in batch. Is there a way to fix this behaviour or will have to live with workaround?
Tried line 5: mediainfo^ --Inform^=Video^;^%Height^%^ "b:\VID\myVideo.avi" But wrong property is returned.
I have this batch which works as expected giving the video height using mediainfo
@echo off
setlocal enableextensions enabledelayedexpansion
REM set "HEIGTH=1081"
for /f "tokens=* delims=" %%j in ('
"mediainfo --Inform=Video;%%Height%% "b:\VID\myVideo.avi""
') do set "HEI=%%j"
echo=!HEI!
Other working way in line 5: mediainfo --Inform^="Video;%%Height%%" "b:\VID\myVideo.avi"
If HEIGHT is already defined as a variable (remove REM to enable it), result will change to HEIGHT content instead (1081 in this example). Couldn't find a working way to avoid %var% replacing and at the same time getting mediainfo to work as expected (it gives all info or nothing, instead of expected Height property of given video). Already read about %% escape and other answers, but couldn't solve this problem. Only workaround is to previously blank HEIGHT var to make sure command will work and avoid all those var names of mediainfo properties used in batch. Is there a way to fix this behaviour or will have to live with workaround?
Tried line 5: mediainfo^ --Inform^=Video^;^%Height^%^ "b:\VID\myVideo.avi" But wrong property is returned.
Share Improve this question asked Feb 1 at 5:53 isidrocoisidroco 431 silver badge5 bronze badges 1- To clarify, mediainfo is case sensitive, batch isn't, correct command syntax to obtain height is: MEDIAINFO --Inform=Video;%Height% "b:\VID\myVideo.avi" – isidroco Commented Feb 1 at 13:23
2 Answers
Reset to default 1The problem occurs, because the command inside FOR/F will be executed in a new cmd.exe instance.
In that instance the command is executed in the command line context instead of a batch file context.
This changes the behavior of the percent expansion.
Undefined variables are not expanded to nothing (like in batch file context), they simply remain unmodified.
To use this you can add a caret (or more carets) to your variable name.
for /f "tokens=* delims=" %%j in ('
"mediainfo --Inform=Video;%%^Hei^gh^t%% "b:\VID\myVideo.avi""
') do set "HEI=%%j"
Now cmd.exe searches for a variable named ^Hei^gh^t
, but removes the carets before it calls mediainfo.
This still fails when you define such a variable
set "^Hei^gh^t=1081"
for /f "tokens=* delims=" %%j in ('
"mediainfo --Inform=Video;%%^Hei^gh^t%% "b:\VID\myVideo.avi""
') do set "HEI=%%j"
But you could use a trampoline jump and use batch file context in your FOR/F loop.
@echo off
REM *** Trampoline jump for function calls of the form ex. "%~d0\:functionLabel:\..\%~pnx0"
FOR /F "tokens=3 delims=:" %%L in ("%~0") DO goto :%%L
setlocal enableextensions enabledelayedexpansion
set "Height=1081"
for /f "tokens=* delims=" %%j in ('call "%~d0\:media:\..\%~pnx0" "b:\VID\myVideo.avi"') do (
set "HEI=%%j"
)
echo=!HEI!
exit /b
:media
echo DEBUG: %* > CON
mediainfo --Inform=Video;%%Height%% %*
exit /b
The advantage is that you can now use mediainfo without having to change your arguments
Really need the syntax of the mediainfo
command to be explicitly stated and the requirement for %height%
to be supplied literally to the command.
I'd suggest that your solution, to empty height
is the correct approach which may be applied in two ways:
saving in and cleaning up a save-variable
SET "washeight=%height%"&SET "height="
for /f "tokens=* delims=" %%j in ('
...
SET "height=%washeight%"&SET "washeight="
SET hei
and executing in a local area and transporting the required data out
SETLOCAL
set "HEIGHT="
for /f "tokens=* delims=" %%j in ('
...
ENDLOCAL&SET "hei=%hei%"
SET hei
本文标签:
版权声明:本文标题:parsing - Get mediainfo Height in Batch: wrong answer returned if property is defined (%% var escape) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741885371a2402989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论