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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
cbo120897Connect With a Mentor Commented:
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
 
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
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:
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
All Courses

From novice to tech pro — start learning today.