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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

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

#1 Post by mgrahek » 09 Sep 2008 18:51

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.

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#2 Post by greenfinch » 10 Sep 2008 10:28

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

Post Reply