Welcome

My name is Jason and I am a software developer in the Bay Area. For fun I like to hike, run, shoot some photos, and take advantage of the many other activities California state has to offer. To the right you will see my resume.

Recent Books
  • Head First Design Patterns
    Head First Design Patterns
    by Elisabeth Freeman, Eric Freeman, Bert Bates, Kathy Sierra, Elisabeth Robson
« Interview question: Finding the most used ASCII character | Main | Redirection of the output streams in DOS scripts »
Sunday
Dec112011

Quick Fix: “Invalid number” error in DOS batch script

I ran into this the other day. After a little searching around I found the answer and thought I'd share the way I resolved it. Hopefully this will shorten someone else's search time.

The error message I was getting was the following: 

Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

It turns out this is happening on the script line that's doing some math with the “set /a” command. It could be happening in other commands as well, but this is where I've seen it.

So here's what you need to know to resolve this. As the error message indicates, it treats values that have leading 0's as octal. Each digit of a octal value is represented with the numbers 0-7. For example octal value 022 is the same as decimal value 18. Why you're getting the error message is because you have a value that has a leading zero, indicating an octal value, but contains at least a digit that is either an 8 or 9. Neither number conforms with how an octal value is represented and this is the source of the error.

Here's the quick solution I came up with. Since I was dealing with dates (months and nth day of the month) I knew that I would have values that are at most 2 digits long and always positive. So before doing any arithmetic on these values I would do a quick check for a leading zero and remove it if present. 

@echo off
(set month=08)
echo The month should be 08: %month%
 
REM The error message should appear. Bummer!
(set /a nextMonth=%month% + 1)
echo The next month should be 9: %nextMonth%
 
REM Checking for leading zero and removing it if present.
if "%month:~0,1%"=="0" (set month=%month:~1%)
echo The month should now be 8: %month%
 
REM No error message. Hooray!
(set /a nextMonth=%month% + 1)
echo The next month should be 9: %nextMonth% 

To break it down, what's happening in the first part of the IF statement is we're obtaining a substring from month whose index starts at 0 and we want the substring to be 1 character long. This means that the code “%month:~0,1%” is just returning the first character of month, which in this case is a zero. Because the IF statement is true, we now set the value of month so that it equals the substring starting from the index of 1 and then all following characters. This means that “%month:~1%” will return the 8. And now the leading zero has been removed and we're good to go!

If you know you're values are only going to be 2 digits long, you could get away with an IF statement like the following:

if %month% lss 10 (set month=%month:~-1%)

I prefer the substring method I initially showed because you could later add some looping logic for removing multiple leading zeros if you were dealing with values like 00789.

Remember, this solution does not handle negative values or values that have more than 1 leading zero. Please take that into consideration when implementing this workaround in your script.

I hope this helps unblock you and clears up any issues you may have been having. If you have another way you handle related scenarios, post it in the comments. I'd love you hear other ways to accomplish this!

Reader Comments (3)

Thanks. Was having exactly the same problem and this was just what I needed.

April 9, 2013 | Unregistered CommenterNJR

Thanks Jason.. great explanation and solution.. helped a bunch..

August 14, 2013 | Unregistered CommenterKobus

Jason,
Thank you so much for your brilliant solution and explanation. I was having the same date problem and was certain that there must be a simpler solution than what I was finding out there.
The best to you!
Eileen

October 8, 2013 | Unregistered CommenterEileen

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>