As @wOxxOm stated in his comment, find
is the perfect choice for this task.
Supposing there is a file test.txt
containing 12 lines, find /V /C "" "C:test.txt"
will output something like:
---------- C:TEST.TXT: 12
So let us use a for /F
loop to capture such an output and string substitution to get the text portion after :
SPACE:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /R "temp\textpipe_tmp\" %%U in ("*.txt") do (
rem capturing the output of `find` here:
for /F "delims=" %%A in ('find /V /C "" "%%~U"') do (
set "NUMBER=%%~A"
rem substituting substring `: ` and everything before by nothing:
set "NUMBER=!NUMBER:*: =!"
)
rem at this point, variable `NUMBER` is available
rem for the currently processed file in `%%~U`:
echo !NUMBER!
)
endlocal
Note that find /V /C ""
will return unexpected reslts if there are empty lines at the end of the file (one of such might not be included in the count). However, empty lines at the beginning or in between non-empty ones will be counted.
Update:
Using redirection like > "C:test.txt" find /V /C ""
rather than find /V /C "" "C:test.txt"
avoids the prefix ---------- C:TEST.TXT:
and just returns the number of lines (like 12
). With this modification no string substitution is necessary and so the code looks like this:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /R "temp\textpipe_tmp\" %%U in ("*.txt") do (
rem capturing the output of `find` here:
for /F "delims=" %%A in ('^> "%%~U" find /V /C ""') do (
set "NUMBER=%%~A"
)
rem at this point, variable `NUMBER` is available
rem for the currently processed file in `%%~U`:
echo !NUMBER!
)
endlocal
The redirection mark <
needs to be escaped like ^<
when being used after in
in for /F
.