Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch - Counting lines in files in current folder and subfolders

This is my script, what it does is count lines from cpp, h, hpp, cs, c files in current folder.

What I want to do is count in subfolders also, but it seems I can't manage to do this.

I made some recursion tries, but I can't implement it in the current code.

call::CountLines Modules\Output\HTML.Tidy\
goto:eof

:CountLines
setlocal
set /a totalNumLines = 0
SETLOCAL ENABLEDELAYEDEXPANSION
for /r %%f in (%~1*.cpp %~1*.h %~1*.hpp %~1*.cs %~1*.c) do (
for /f %%C in ('Find /V /C "" ^< %%f') do set Count=%%C
set /a totalNumLines+=!Count!
)

echo Total number of cod lines for %~1: %totalNumLines% >> log.txt

Please let me know if you know a solution or a better way.

Regards,

Stefan

like image 601
Gerald Hughes Avatar asked Mar 20 '23 15:03

Gerald Hughes


1 Answers

Path information must not be within IN() clause when using FOR /R. The root path should follow the /R option instead.

@echo off

:CountLines
setlocal
set /a totalNumLines = 0
for /r %1 %%F in (*.cpp *.h *.hpp *.cs *.c) do (
  for /f %%N in ('find /v /c "" ^<"%%F"') do set /a totalNumLines+=%%N
)

echo Total number of code lines for %1 = %totalNumLines% >>log.txt

I don't remember the difference, but type file|find /c /v "" and find /c /v "" <file can give different results. I don't remember what the trigger condition is, or which is better.

like image 136
dbenham Avatar answered Apr 06 '23 06:04

dbenham