简体   繁体   中英

Modify Windows batch file to count lines

I have this code:

@echo off

:CountLines
setlocal
set /a totalNumLines = 0
for /r %1 %%F in (*.txt) do (
  for /f %%N in ('find /v /c "" ^<"%%F"') do set /a totalNumLines+=%%N
)

echo Total number of code lines for %1 = %totalNumLines% >>log.txt

This counts all the lines in the files and gives me the total number of lines. However, I need it to give the filename and lines for each file, not the total.

the desired output is a bit like this:

FILEA.txt 1200
FILEB.txt 300
FILEC.txt 10

Your answer doesn't output the filename:

@echo off & Setlocal EnableDelayedExpansion
Set /A TotalNumLines=0
( For /R %1 %%F In (*.Txt) Do (
      For /F %%N In ('Find /V /C "" ^<"%%F"') Do (
          Echo %%F %%N
          Set /A TotalNumLines+=%%N
      ) 
  )
  Echo Total number of code lines for all files = !TotalNumLines!
) > log.txt
EndLocal

I found the answer myself after a lot of trial and error:

@echo off

:CountLines
setlocal
set /a totalNumLines = 0
for /r %1 %%F in (*.txt) do (
  for /f %%N in ('find /v /c "" ^<"%%F"') do echo %%N >>log.txt
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM