1
votes

I'd like to compute the average number of lines of specific files from CMD. To find the number of lines of one file I've got:

findstr /R /N "^" "FILENAME" | find /C ":"

So I'd have something like this:

setlocal enabledelayedexpansion
set sum = 0
for /l %%x in (1, 1, 10) do (
    set tmpnum = findstr /R /N "^" "file-%%x.csv" | find /C ":"
    set /a sum=sum+tmpnum
)
echo %sum%/10
endlocal

The problem is that sum is always 0 and I believe tmpnum does not get the correct value assigned.

1

1 Answers

3
votes

The set tmpnum = line is wrong, you cannot set a variable to the output of a command like that. The correct syntax is

for /l %%x in (1, 1, 10) do (
    for /f %%c in ('findstr /R /N "^" "file-%%x.csv" ^| find /C ":"') do (
        set /a sum=sum+%%c
    )
)   

Of course the echo %sum%/10 will also not do the math -- you need another SET /A for that.