Increase four letter/number variable

Microsoft Windows xp professional w/serv...
February 24, 2010 at 00:23:17
Specs: Windows XP
Hi.
I have a four character variable which I need to increase with 1 (number) for each loop...

It starts as "0001", and can go up to "9999" (duh).

Problem is, DOS gives me an error if I try to add +1 to 0001, saying it's not numeric.

So, I tried this:

SET varCount=%varCount:~-1%
SET /A varCount= %varCount% + 1

...but that only works for 2-9 of course...

Anyone know how to solve this?


See More: Increase four letter/number variable

Report •


#1
February 24, 2010 at 02:58:00
You need to strip leading zeros, increment, that re-pad out to 4 places.

Because some brainiac decided that 0N is octal. LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#2
February 24, 2010 at 05:59:44
In your case, the easiest way to strip the zeros would be to do this:

set varCount=0000%varCount%
set /a varCount=1%varCount:~-4% - 10000


Report •

#3
February 24, 2010 at 07:21:14
Since I'm doing this repeatedly through the code, I ended up with two functions:

:myLeftTrim string char 
::	-- strips white spaces (or other characters) from the beginning of a string
::  -- string [in,out] - string variable to be trimmed
::  -- char   [in,opt] - character to be trimmed, default is space
SETLOCAL ENABLEDELAYEDEXPANSION
call set "string=%%%~1%%"
set "charlist=%~2"
if not defined charlist set "charlist=0"
for /f "tokens=* delims=%charlist%" %%a in ("%string%") do set "string=%%a"
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" SET "%~1=%string%"
)
EXIT /b
GOTO:EOF


:myAddLeadingZeroes string
::	--	returns input four character string with leading zeroes
::  --	string [in,out] - string variable to which will be returned
SETLOCAL ENABLEDELAYEDEXPANSION
call set "string=%%%~1%%"
SET string=0000%string%
SET string=%string:~-4%
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" SET "%~1=%string%"
)
EXIT /b
GOTO:EOF

Works like a charm :)


Report •

Related Solutions


Ask Question