@echo off ECHO Lines of Code > tmpLOC.txt ECHO Filename , LOC > tmpLOC.csv REM The following line sets delayed expansion, which is used to make sure variables REM have real dynamic value. SETLOCAL ENABLEDELAYEDEXPANSION SET TotalLOC = 0 REM Get DIR from input SET DIR=%1 for /f "tokens=*" %%i in ('dir /b /a-d %DIR%') do ( for /f "tokens=*" %%j in (%DIR%\%%i) do ( set /a numLines=!numLines!+1 ) SET /a TotalLOC = TotalLOC + !numLines! echo %%i !numLines! >> tmpLOC.txt echo %%i , !numLines! >> tmpLOC.csv ) echo Grand Total >> tmpLOC.txt echo %TotalLOC% >> tmpLOC.txt echo Total Lines of Code %TotalLOC%
Usage:
All code files are in C:\project\source, for example.
Save above batch file as CountLOC.bat in C:\project.
At cmd:
c:\> CD C:\project
c:\project> CountLOC source
Total LInes of Code 4490061
Results will be in tmpLOC.txt and tmpLOC.csv
NOTES:
The ! , exclamation point, in DOS Batch files is a concept called delayed expansion, and is used for doing Loops, since by default DOS Batch files would just print the initial value of the variable)
See:
http://batcheero.blogspot.com/2007/06/how-to-enabledelayedexpansion.html
For explanation of the /a after SET:
http://www.robvanderwoude.com/battech_math.php
ht http://blog.brunobrant.net/2008/08/how-to-count-lines-of-text-loc-in.html