Set Conter 5 digit

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Set Conter 5 digit

#1 Post by darioit » 10 Sep 2015 04:31

Hello folks, I have this simply work, add 1 unit to my file Counter.txt, but digit must be 5 digit fix (ex.00023)

This is my script, but when decimal is moving from 1 position doesn't works, any idea?

Code: Select all

echo on
setlocal EnableDelayedExpansion
set Counter=0
FOR /f "tokens=1" %%i in (Counter.txt) do set /a Counter = %%i
echo %Counter%
set /a Counter=%Counter%+1
set NewCounter=00000!Counter!
set NewCounter=!NewCounter:~-5!
ECHO %NewCounter% > Counter.txt


Thanks in advance

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Set Conter 5 digit

#2 Post by jeb » 10 Sep 2015 04:39

Hi darioit,

the problem is that numbers with leading zeros are treated as octal values.
So 00010 will be translated to 8 decimal.

You only need to do a little trick.
Prefix your number with a "1" and then take a modulo of the number.
ex.
00008 -> 100008
100008 modulo 100000 -> 8

Code: Select all

FOR /f "tokens=1" %%i in (Counter.txt) do set /a Counter= 1%%i %% 100000

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Set Conter 5 digit

#3 Post by darioit » 10 Sep 2015 06:11

Perfect works fine, many thanks!

Post Reply