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

前端 未结 16 1149
青春惊慌失措
青春惊慌失措 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:07

    The :countLines subroutine below accepts two parameters: a variable name; and a filename. The number of lines in the file are counted, the result is stored in the variable, and the result is passed back to the main program.

    The code has the following features:

    • Reads files with Windows or Unix line endings.
    • Handles Unicode as well as ANSI/ASCII text files.
    • Copes with extremely long lines.
    • Isn’t fazed by the null character.
    • Raises an error on reading an empty file.
    • Counts beyond the Batch max int limit of (31^2)-1.
    @echo off & setLocal enableExtensions disableDelayedExpansion
    
    call :countLines noOfLines "%~1" || (
        >&2 echo(file "%~nx1" is empty & goto end
    ) %= cond exec =%
    echo(file "%~nx1" has %noOfLines% line(s)
    
    :end - exit program with appropriate errorLevel
    endLocal & goto :EOF
    
    :countLines result= "%file%"
    :: counts the number of lines in a file
    setLocal disableDelayedExpansion
    (set "lc=0" & call)
    
    for /f "delims=:" %%N in ('
        cmd /d /a /c type "%~2" ^^^& ^<nul set /p "=#" ^| (^
        2^>nul findStr /n "^" ^&^& echo(^) ^| ^
        findStr /blv 1: ^| 2^>nul findStr /lnxc:" "
    ') do (set "lc=%%N" & call;) %= for /f =%
    
    endlocal & set "%1=%lc%"
    exit /b %errorLevel% %= countLines =%

    I know it looks hideous, but it covers most edge-cases and is surprisingly fast.

    0 讨论(0)
  • 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%
    
    0 讨论(0)
  • 2020-11-27 15:08

    One nice surprise is for one who has git bash on his windows: just plain old linux wc -l <filename> will works for you there

    0 讨论(0)
  • 2020-11-27 15:10

    Inspired by the previous posts, a shorter way of doing so:

    CMD.exe
    C:\>FINDSTR /R /N "^.*$" file.txt | FIND /C ":"
    

    The number of lines

    Try it. It works in my console.

    EDITED:

    (the "$" sign removed)

    FINDSTR /R /N "^.*" file.txt | FIND /C ":"
    

    $ reduces the number by 1 because it is accepting the first row as Field name and then counting the number of rows.

    0 讨论(0)
  • 2020-11-27 15:10

    Just:

    c:\>(for /r %f in (*.java) do @type %f ) | find /c /v ""
    

    Font: https://superuser.com/questions/959036/what-is-the-windows-equivalent-of-wc-l

    0 讨论(0)
  • 2020-11-27 15:12
    for /f "usebackq" %A in (`TYPE c:\temp\file.txt ^| find /v /c "" `) do set numlines=%A

    in a batch file, use %%A instead of %A

    0 讨论(0)
提交回复
热议问题