deed02392
deed02392

Reputation: 5022

Batch command script, invalid numeric with leading zero?

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
)

Upvotes: 0

Views: 1687

Answers (2)

dbenham
dbenham

Reputation: 130879

parkydr provided the correct diagnosis as to why you are getting the error - numbers prefixed with 0 are interpreted as octal.

There is a simple solution - change the right side to 08 and 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.

if "!cam!" leq "08" ...

EDIT

The solution above doesn't directly address the failure of SET /A when %%c is 08 or 09. My solution above assumes the /A option has been removed.

The cam variable can be eliminated altogether:

if "%%c" leq "08" ...

Even better yet, the extra non-numeric character(s) is not needed because IF sees 08 as invalid octal notation and defaults to a string comparison.

if %%c leq 08 ...

See Rules for how CMD.EXE parses numbers for more info. There is a section dedicated to the IF statement.

Upvotes: 2

parkydr
parkydr

Reputation: 7782

Numbers with leading zeros are interpreted as octal (like 0x indicates hex) so 08 and 09 are bad octal values.

Upvotes: 2

Related Questions