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
number.
How do I get the value from the registry
variable and how do I change it in my
DOS batch file ?
karthikeshAsked:
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.

cbo120897Commented:
Hi,
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
0

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
cbo120897Commented:
Hi,
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%
pause

---------------------------------------

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

0
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:
Hi GBO,

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.

and

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

Please reply. Thanks.
0
carmineCommented:
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....
0
cbo120897Commented:
Hi,
you can't use regedit.exe !

Try to download reg.ex http://mspress.microsoft.com/reslink/nt40/toolbox/tools/reg.htm

bye cbo
0
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.