Saturday, August 3, 2019

windows - Why this multiline for/in/do doesn't work in a batch?


To help my kid learn the most common colors, I made this batch (it's Windows 7 but I suppose it's the same on another Windows version):


@echo off
for %%a in (^
black ^
blue ^
green ^
gray ^
orange ^
pink ^
red ^
#82461F ^
#7E287E ^
white ^
yellow ^
) do ffplay -hide_banner -f lavfi -i color=%%a:160x90:d=3,format=rgb24 -fs

Pretty straightforward: the for loop runs every ffplay command one at a time, filling my 16:9 screen with a single color until the kid recognizes the color and names it well; then I press q to exit that instance of ffplay and the next color is displayed.


I break the command so I can have every color value in a line to easily mix the lines when I need it in order to avoid a specific order to be learned.


But it just doesn't work. If I get right from these two questions...


... since every ^ disappears from the final command, my batch is equivalent to the command line interpreter single line...


for %a in (black blue green gray orange pink red saddlebrown violet white yellow ) do ffplay -hide_banner -f lavfi -i color=%a:160x90:d=3,format=rgb24 -fs

... which, needless to say, works perfectly.


What it does work is beginning the last line with a space. But, in spite of finding a way to make the batch work, the question remains. Why is it necessary that space? Or, what am I missing from the answers to the listed questions?


Edit:
#82461F==brown
#7E287E==violet
My wife wasn't convinced of how these two named colors looked on our display.


Edit:
For reference, the final working batch (thanks all), with colors alphabetically ordered and without unnecessary carets and spaces:


@echo off
for %%a in (
black
blue
#82461F %= brown %
gray
green
orange
pink
red
#7E287E %= violet %
white
yellow
) do ffplay -hide_banner -f lavfi -i color=%%a:160x90:d=3,format=rgb24 -fs

Answer



You can omit all the line continuation chars ^ they aren't neccessary,
but as the lines following start in the first column this is interpreted as escaping the first char of the following line - not what you itended I guess.


That doesn't harm for letters, but escaping the closing parentheses is a different thing.
The following batch echoes only the command for testing purposes:


@echo off
for %%a in (black
blue
green
gray
orange
pink
red
#82461F
#7E287E
white
yellow) do echo ffplay -hide_banner -f lavfi -i color=%%a:160x90:d=3,format=rgb24 -fs

Sample output:


ffplay -hide_banner -f lavfi -i color=black:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=blue:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=green:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=gray:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=orange:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=pink:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=red:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=#82461F:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=#7E287E:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=white:160x90:d=3,format=rgb24 -fs
ffplay -hide_banner -f lavfi -i color=yellow:160x90:d=3,format=rgb24 -fs

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...