I’m trying to generate a script to run ffmpeg on multiple video card inputs by using a loop in batch. So far I have:
This is working great except for when it tries to do:
set /a cam=%%c when c="08" or c="09". I get:
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
What gives? Here’s the code:
@echo off
setlocal enabledelayedexpansion
for %%c in (01 02 03 04 05 06 07 08 09 10 11) do (
set /a cam=%%c
if !cam! leq 8 (
echo foobar_sd%%c
) else (
echo barfoo_hd%%c
)
pause
)
parkydr provided the correct diagnosis as to why you are getting the error – numbers prefixed with
0are interpreted as octal.There is a simple solution – change the right side to
08and add a non numeric character to both sides of the comparison to force a string comparison instead of a numeric comparison. The values sort properly as strings because they are zero padded to all have the same length. I like to use quotes, but nearly any character will do.EDIT
The solution above doesn’t directly address the failure of
SET /Awhen%%cis08or09. My solution above assumes the/Aoption has been removed.The
camvariable can be eliminated altogether:Even better yet, the extra non-numeric character(s) is not needed because IF sees
08as invalid octal notation and defaults to a string comparison.See Rules for how CMD.EXE parses numbers for more info. There is a section dedicated to the IF statement.