Friday, July 29, 2011

Count lines in multiple files using Windows command prompt

Not really Oracle-related, but I'm posting this as a reminder to myself and possibly useful to others.

To count the number of lines in a given set of files using the Windows command prompt, do the following:

for %G in (*.sql) do find /c /v "_+_" %G

This invokes the "find" command once for each file, counting the lines that do NOT contain the string "_+_" (the string has no special significance, any weird string that would not occur "naturally" in the files can be used).

There are probably more sophisticated ways of doing this, perhaps using PowerShell and whatnot. Leave a comment if you know of other methods (something that adds together all the file line counts into one grand total would be even better).


Jake Robinson said...

Powershell method with grand total: Get-ChildItem *.sql | where-object {$ -notmatch '+'} | get-content | measure-object

Anonymous said...

I like to use cygwin (a unix/linux emulator for Windows) for this type of thing:

wc *.sql

wc -l *.txt
for just the number of lines

Anonymous said...

With help from code from Tom Lavedas at, I wrote this batch file which displays both number of lines for each file and total number of lines for all files.

@echo off
if not [%1]==[] pushd %1
for /r %%F in (*.sql) do call :sub "%%F"
echo Total lines in %Files% files: %Total%
exit /b 0
set /a Cnt=0
for /f %%n in ('type %1') do set /a Cnt+=1
set /a Total+=Cnt
set /a Files+=1
echo %1: %Cnt% lines

Anmol Kaushik said...

^^ Results into a lock. Have to terminate the batch.