[BATCH] Count number of lines in a text file

Solved
-Tyrael- Posted messages 103 Status Member -  
 Hakim -
Hello everyone,

There are many topics, and I've (almost) found my happiness.
I still have a problem that I can't solve.

The line count is incorrect. I'm missing more than 30 lines.
Here's my code:

@echo off set nb=0 set /p fichier="Drag your file here" for /f "tokens=*" %%i in ('type %fichier%') do ( set /a nb+=1 ) echo %nb% pause>nul


The file is a *.log file; I don't know if that changes anything compared to a *.txt
It has 191 lines when opened with Notepad and 201 when opened with Notepad++.

Configuration: Windows XP / Safari 536.11

7 answers

  1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    hello
    try this to count the lines
    find /v /c "" < file
    10
  2. Demonikar Posted messages 18 Status Member 10
     
    I think I have the solution:

     @echo off setlocal enabledelayedexpansion set nb=0 set /p fichier="Drag your file here" for /f "delims=" %%a in (%fichier%) do ( set /a nb=!nb!+1 ) echo %nb% pause>nul endlocal 
    1
    1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
       
      this loop ignores empty lines in the file
      for /f "delims=" %%a in (%fichier%) do ( 
      1
    2. Demonikar Posted messages 18 Status Member 10
       
      you're not wrong...
      0
  3. Hakim
     
    hello,

    if I want to count the number of lines in several TXT files, how would I do that please?

    I did it like this but it doesn't work

    @Echo OFF

    for /f "delims=;" %%a in (fichier.txt) do (

    for /f "delims=" %%i in ('type %%a.txt') do ( set Compt=0 set /a Compt+=1
    echo %Compt% >>resultats.txt
    )
    )

    pause
    1
  4. -Tyrael- Posted messages 103 Status Member 10
     
    Indeed, I have the right number of lines (the one from np++) thank you :)
    How can I store this number in a variable?

    The end goal is to retrieve the first 3 and the last 3 lines of the file to put them in a log file.
    0
  5. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    this loop ignores empty lines in the file
    for /f "tokens=*" %%i in ('type %file%') do 
    pass through a loop to store the result in a variable
    C:> type f1.txt aa bb cc dd C:> find /v /c "" <f1.txt c:=""> for /f %a in ('find /c /v "" ^< f1.txt') do @set l=%a C:> echo %l% 7 C:</f1.txt>
    0
  6. -Tyrael- Posted messages 103 Status Member 10
     
    Perfect !!
    (just needed to use %%a instead of %a)

    Thank you very much!

    Just need to get my lines and it's all good.
    0
  7. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    yes, %a in interactive mode, %%a in a .bat file
    0