Numeric addition in DOS batch files

I would like to generate a sequence
number in a DOS .bat batch file whenever
it is run. So to remember the last count
I want to use a environment (registry)
variable and increment it every time the
batch file is run to get the next sequence
How do I get the value from the registry
variable and how do I change it in my
DOS batch file ?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

cbo120897Connect With a Mentor Commented:
I think this is a hard question for only 25 points :-)

Here are a batch who can add a value to a environment variable. For next 25 points I show you how to read a registry variable:

@echo off
set old=66 &REM oly the test value
set add=1  &REM the value to add
set /A new=old+%add% &REM the addition
echo %new% &REM the old value + add
because this is a tricky question I have finished the solution:

I have store the counter at HKLM\SOFTWARE\bob\count. Next I create the following batch. Attention! After "delims=" there is a tabulator and a space ("<tab> ").

You need the reg.exe from the ressource kit to read and wrote to the registry...


@echo off

REM get the value from the registry
for /f "tokens=1-3 delims=       " %%a in ('reg.exe query HKEY_LOCAL_MACHINE\SOFTWARE\bob\count') do set counter=%%c

REM decrement the value
set /A counter=counter+1

REM store the new value to the registry
reg.exe update HKEY_LOCAL_MACHINE\SOFTWARE\bob\count=%counter% >NUL

REM a test
echo %counter%


How about some points more :-)
- there is only ONE line at for /f.... !!
- and one line at reg.exe update .... !!
- remember the tabulator !!

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

karthikeshAuthor Commented:

I tried your solution.

I don't have reg.exe but tried with
regedit.exe. Is it OK?

I Created a new Key  RCVFNAME under HKEY_LOCAL_MACHINE\SOFTWARE and a string value COUNT under it. I assigned
a value 1 for COUNT.

And I got the following errors :
 Cannot import query: Error opening the
 file. There may ne a disk or file
 system error.


 Cannot import HKEY_LOCAL_MACHINE\SOFTWARE\RCVFNAME\COUNT : Error opening the file. There may be a disk or file system error.

Please reply. Thanks.
Without a reg tool you'd be best saving the count in file.

First create a file (say count.txt) that contains the required starting sequence number.

Then base your script on the following...

REM path to directory where count file is kept.
cd \count

REM Get value from file...
for /f %%a in ('type count.txt') do set count=%%a

REM Increment count
set /a count=%count%+1

REM Save new count
echo %count%>count.txt

REM Use count....
you can't use regedit.exe !

Try to download reg.ex

bye cbo
All Courses

From novice to tech pro — start learning today.