CMD: Can't add 1 to 08 using += operator

2019-08-26 10:28发布

问题:

I am using CMD commands to work on some variables. I can add 1 to a variable but if that variable is 08 or 09, the result comes out to be 1,rather than 9 or 10. Here's the code:

setlocal EnableDelayedExpansion
set day=08
echo Day:%day%
set /a day+=1
echo Day:% day%

This works for numbers 1-7, and after 10. I need to add a leading zero to digits less than 10 because otherwise somehow the variable is not written to a text file (which I perform later) despite showing the correct value in other parts of the code.

回答1:

Help for command SET can be read by running in a command prompt window set /?. It explains that numbers with a leading 0 are interpreted as octal numbers, see function strtol with base being 0 (automatic detection) as used internally by Windows command interpreter.

08 and 09 are invalid in octal number system and are therefore interpreted with value 0.

One solution for this case is using first a string concatenation during preprocessing state to change the string 08 to 108 and subtract 99 from 108 converted to an integer number to get 08 and 09 as well as 01 to 31 incremented by 1.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "day=08"
echo Day: %day%
set /A day=1%day% - 99
echo Day: %day%
endlocal

The output of above batch file is:

Day: 08
Day: 9

Delayed environment variable expansion is not needed here and in general not needed at all in an arithmetic expression as also explained by help of command SET. There are of course exceptions like a string concatenation must be done first before executing the arithmetic expression within a command block on which the environment variable was defined or modified before in the command block. But those use cases are really rare.

When the result should be again with a leading 0 for numbers with less than two digits, it is best not subtracting 99, but adding 1 and assign to the environment variable in next step just the last two digits from the string with always 3 digits after evaluation of arithmetic expression.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "day=08"
echo Day: %day%
set /A day=1%day% + 1
set "day=%day:~-2%"
echo Day: %day%
endlocal

The output of above batch file is:

Day: 08
Day: 09

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • set /?
  • setlocal /?


回答2:

Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.

From set /?.