Thursday, July 25, 2013

Count Lines of Code with DOS Batch file

@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.

SET TotalLOC = 0
REM Get DIR from input

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%

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

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)

For explanation of the /a after SET:


No comments: