kaetzacoatl
kaetzacoatl

Reputation: 1469

Batch increment values

I try to read a value from a file and increment it. But if I increment a value bigger than 8 it results to 1. Here's my code:

for /f "delims=" %%i in (VERSION.txt) do set ver=%%i
set /a ver+=1
echo 0%ver%>>VERSION.txt
echo Build: %ver%

My file looks like:

06
07
08
01
02
03

Can somebody explain what is wrong?

Upvotes: 0

Views: 257

Answers (3)

Stephan
Stephan

Reputation: 56180

As Joey already stated, any number with a leading zero is interpreted as octal. But you can do the calculation by adding a one in front of it and strip it afterwards:

...
set /a ver=1%%i+1
echo %ver:~1%
...

run it with echo on to see, what happens.

Upvotes: 0

SachaDee
SachaDee

Reputation: 9545

You have to save you're value without the 0 :

test :

set /a toto=08+1

for the interpreter this is not a decimal,hexa or octal valid value.

You'll better do something like this :

@echo off
for /f "delims=" %%i in (VERSION.txt) do set ver=%%i
set /a ver+=1
echo %ver%>VERSION.txt
echo Build: 0%ver%

EDIT :

You can make like this :

@echo off
for /f "delims=" %%i in (VERSION.txt) do set "ver=%%i"
if  "%ver:~0,1%"=="0" set "ver=%ver:~1%"
set /a ver+=1
echo 0%ver%>>VERSION.txt
echo Build: 0%ver%

Upvotes: 2

Joey
Joey

Reputation: 354536

Numbers with a leading zero are interpreted as octal numbers. 08 is not a valid octal number.

Upvotes: 1

Related Questions