2

I need to count, in a file, the number of times a batch script has been executed.

In linux shell, this would be something like

counter=`cat buildnumber.txt`;
counter=`echo $counter+1|bc`
echo $counter > buildnumber.txt

but how does one do this in a batch file?

3

2 Answers 2

3

exactly the same logic, but using batch commands:

<buildnumber.txt set /p counter=
set /a counter +=1
echo %counter%>buildnumber.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Is it really <buildnumber.txt and then set? Looks a bit screwed up... Just curious.
@geisterfurz007 it doesn't matter where you put the redirection. set /p counter= <buildnumber.txt works exactly the same. (run with echo on to see, that the parser interprets both possibilities the same way)
1

This is my approch to count the number of execution for the batch script :

@echo off
Setlocal enabledelayedexpansion
Title Count the number of times my BATCH file is run
Mode Con Cols=60 lines=3 & color 0E
set /a count=1
set "FileCount=%tmp%\%~n0.txt"
If Not exist "%FileCount%" (
    echo !count! > "%FileCount%"
) else (
    For /F "tokens=*" %%a in ('Type "%FileCount%"') Do (
        set /a count=!count! + %%a
        echo !count! > "%FileCount%"
    )
)
echo.
echo        This batch script is running for "!count! time(s)"
EndLocal
pause>nul

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.