Windows if full of undocumented gizmo’s, like find
alternative for wc -l
counting all lines in a file: [WayBack] File Line Count
Use
FIND
command to count file lines, store line count into a variable.
Description: Running the FIND command with option /v and empty search string will find all lines
Running the FIND command with option /c will output the line count only.
The FOR command with option /f will parse the output, the line count in this case, and the set command put the line number into the cnt variable.Script:
1.
2.
3.
4.
set file=textfile.txt
set /a cnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a cnt=%%a
echo %file% has %cnt% lines
Script Output:
Script Output textfile.txt has 50 lines
[WayBack] Stupid command-line trick: Counting the number of lines in stdin | The Old New Thing
Windows doesn’t come with
wc
,
but there’s a sneaky way to count the number of lines anyway:some-command-that-generates-output | find /c /v ""It is a special quirk of the
find
command
that the null string is treated as never matching.
The/v
flag reverses the sense of the test,
so now it matches everything.
And the/c
flag returns the count.…
The reason dates back to the original MS-DOS
version offind.exe
,
which according to the comments appears to have been written
in 1982.
And back then, pretty much all of MS-DOS was written in assembly
language.…
Via: batch file line count – Google Search and [WayBack] windows – How to count no of lines in text file and store the value into a variable using batch script? – Stack Overflow
–jeroen