How to count no of lines in text file and store the value into a variable using batch script?

前端 未结 16 1248
青春惊慌失措
青春惊慌失措 2020-11-27 14:28

I want to count the no of lines in a text file and then the value has to be stored into a environment variable. The command to count the no of lines is

findstr /R         


        
16条回答
  •  生来不讨喜
    2020-11-27 15:08

    You could use the FOR /F loop, to assign the output to a variable.

    I use the cmd-variable, so it's not neccessary to escape the pipe or other characters in the cmd-string, as the delayed expansion passes the string "unchanged" to the FOR-Loop.

    @echo off
    cls
    setlocal EnableDelayedExpansion
    set "cmd=findstr /R /N "^^" file.txt | find /C ":""
    
    for /f %%a in ('!cmd!') do set number=%%a
    echo %number%
    

提交回复
热议问题