Question

Windows XP

My batch file runs a command that has several lines of output. How can I count (and store in a variable) the lines of output without ever writing to the disk?

Was it helpful?

Solution

Here's sample script that will count the lines in the output of the dir command.

@echo off
setlocal enabledelayedexpansion

set lc=0

for /f "usebackq delims=_" %%i in (`dir`) do (
  echo %%i
  set /a lc=!lc! + 1
)

echo %lc%

endlocal

You can substitute dir with your command and you can use quotes and specify parameters. You will have to escape some other characters though - ^, | < > and &.

If you need to not only count the lines, but also parse each line, you might have to change the token delimiter from _ (as I used in the example) to something else that will not result in the line split in multiple tokens.

OTHER TIPS

dir | find /v /c "zzzxxx"

gives a line count

and while you are at it, you can also download GNU packages(coreutils) for windows and use the wc tool: eg

dir | wc -l
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top