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?

Alexander
  • 19,906
  • 19
  • 75
  • 162
  • is using `bc` for such simple calculation a bit overkill? – phuclv Sep 09 '16 at 11:17
  • Dunno whether there is a less overkill-y calculator available in bash... – Alexander Sep 09 '16 at 12:23
  • 1
    yes, why not? [Math Commands](http://tldp.org/LDP/abs/html/mathc.html) http://unix.stackexchange.com/q/40786/44425 http://stackoverflow.com/q/1088098/995714 – phuclv Sep 09 '16 at 13:19

2 Answers2

3

exactly the same logic, but using batch commands:

<buildnumber.txt set /p counter=
set /a counter +=1
echo %counter%>buildnumber.txt
Stephan
  • 53,940
  • 10
  • 58
  • 91
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
Hackoo
  • 18,337
  • 3
  • 40
  • 70