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)


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

::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 •

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:\
IF NOT EXIST C:\counter.txt GOTO makefile
set /p total=<counter.txt
set /a total+=1
echo %total%>counter.txt
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