Counter for every start of batch file

September 21, 2010 at 19:54:06
Specs: Windows Server 2008, 4GB
I need help to perform this code to extract a value in a file text and then change that value to another one.
This script creates a counter any time that the batch is executed and saved/replaced the info into a file –data.txt-.

:: The file with the value for “EXEC” (data.txt)
DATA1=VALUEA
DATA2=VALUEB
DATA3=VALUEC
EXEC=0

Example
-------------

:: Find the value
@echo oFF
setLocal EnableDelayedExpansion
for /f "delims=" %%a in (data.txt) do (
set %%a
%data1%
%data2%
%EXEC%
)

::this find the line with the “EXEC” value
Set filemod=4

::If the value is different to “0”, take the value and add 1
if %EXEC% neq 0 (
set /a contexec=%EXEC%+1
set newexec=%cont%
for /f "tokens=* delims= " %%a in (data.txt) do (
set /A contlines=!contlines!+1
if !contlines! neq %filemod% (
echo %%a>>newdata.tmp
)
)

:: inserted the new value into the temporally file (EXEC=1)
echo EXEC=%newexec%>>newdata.tmp
)

:: created a new file (data.txt) with updated values
del data.txt
ren newdata.tmp data.txt


See More: Counter for every start of batch file

Report •


#1
September 29, 2010 at 22:19:49
Well I kind of understand what you're trying to do. If I'm taking your question the right way, this should help.

cd C:\
:start
IF NOT EXIST C:\counter.txt GOTO makefile
set /p total=<counter.txt
set /a total+=1
echo %total%>counter.txt
exit
:makefile
echo 100>counter.txt
GOTO start

This simple script adds 1 to the counter in "C:\counter.txt" each time it's run.


Report •
Related Solutions


Ask Question