Page 1 of 1

How do I make a variable that "counts" and saves t

Posted: 09 Sep 2008 18:51
by mgrahek
I want to have a counter in my batch file. I want it to count the number of times the program has run, or the number of times that the sub section of the batch file is executed.

Is it possible to have the program modify itself as it is run and then have the changes saved and made permanent?

I figured that I could do this with an external text file, but I would like to keep this as a single file.

Posted: 10 Sep 2008 10:28
by greenfinch
I think the problem with a cmd script modifying itself is that the system locks the file in use.

You could certainly do it with an external text file, which the batch file read in at the start, and writes out again at the end.

You can use SET /a for simple counters:
http://www.dostips.com/DtTipsArithmetic.php

This is handy to read in a text file where you have pairs like:

varA=1
varB=2
varC=3 etc

Code: Select all

FOR /F "DELIMS=" %%a IN ('FINDSTR /R "=" "textfile.ini"') DO SET %%a


This reads textfile.ini, and sets a variable for every X=Y pair (set varA=1, set varB=2 etc)

You then change these in the program with your counter, and write them out again at the end, e.g. with

Code: Select all

type varA=2>textfile.ini
type varB=3>>textfile.ini