Reputation: 55
I'm extracting two digits and adding one to it. But when the last two digits are 08 it's throwing the above error.
set last_two=!file_name:~-2,2!
set /a add_millisec=!last_two!+1
set add_millisec=0!add_millisec!
set add_millisec=!add_millisec:~-2!
Can someone please check and help me here...
Upvotes: 1
Views: 2507
Reputation: 34949
Open a command prompt window and type set /?
:
[...] 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. [...]
You will find that set /A
treats numbers with leading 0
as octal numbers.
To overcome this, you can do the following:
Prefix the number with 1
and remove it again after the calculations:
set last_two=1!file_name:~-2,2!
set /A add_millisec=last_two+1
set add_millisec=!add_millisec:~-2!
For this you need to know the total number of digits in advance.
Remove the trailing zeros before any calculations and pad the result with leading zeros afterwards as needed:
set last_two=!file_name:~-2,2!
rem The following line removes all leading zeros:
for /F "tokens=* delims=0" %%Z in ("!last_two!") do set last_two=%%Z
set /A last_two+=0 & rem this avoids `last_two` to be empty if it is `0`
set /A add_millisec=last_two+1
set add_millisec=0!add_millisec!
set add_millisec=!add_millisec:~-2!
Or:
set last_two=!file_name:~-2,2!
rem The following two lines remove all leading zeros:
cmd /C exit !last_two!
set /A last_two=!ErrorLevel!
set /A add_millisec=last_two+1
set add_millisec=0!add_millisec!
set add_millisec=!add_millisec:~-2!
This method is more flexible as you do not know the number of digits.
Upvotes: 3