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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 !!

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.