user3108268
user3108268

Reputation: 1083

Windows CLI doesn't show correct variables with spaces

There is this command line tool for MediaInfo that we use to print video file information. E.g. I want to get video file duration value:

mediainfo --Inform=General;%Duration/String2% video.mkv
46 min 57 s

As you can see it prints 46 min 57 s which is correct.

Now lets say I want to put that value into a variable. So we run this:

for /f "usebackq" %a in (`"mediainfo --Inform=General;%%Duration/String2%% video.mkv"`) do set duration=%a
set duration=%46

As you can see it gives set duration=%46 on a new line? And if I type next %duration% I get:

'%46' is not recognized as an internal or external command,
operable program or batch file.

What is going on? How do I get my %duration% variable show 46 min 57 s?

Upvotes: 0

Views: 374

Answers (2)

Sam Denty
Sam Denty

Reputation: 4085

In a batch file:

@echo off
for /f "tokens=*" %%a in ('mediainfo --Inform=General;%Duration/String2% video.mkv') do set duration=%%a
echo %duration%
pause

In CMD

for /f "tokens=*" %a in ('mediainfo --Inform=General;%Duration/String2% video.mkv') do set duration=%a
echo %duration%

Output:

46 min 57 s

Upvotes: 1

Magoo
Magoo

Reputation: 79982

for /f "usebackqDELIMS=" %%a

The default delimiters include space. This syntax disables delimiters so the entire line is assigned.

The metavariable a needs to have 2 % if you are running a batch file containintg this line. If you are running from the prompt, you need %a. This applies to the assignment instruction as well.

No idea what you are attempting to do with the %46 malarkey. Please explain.

Upvotes: 1

Related Questions