Count number of lines in a file using DOS

Every once in a while I get on a computer and I need to count the number of lines in a file. My first instinct is to open my text editor (editplus) and hit ctrl+end to get to the bottom of the document. Then I can view the status bar which will tell me the line number. This works fine when I am on my computer but not when I am on another computer that does not have editplus installed.

My next option might be to open this file in notepad and do the exact same thing. This will work fine if the file is not to large. The problem is I often deal with very large files. I need a quicker way to produce the same results.

This is where DOS comes into play. I can use the following command and let DOS quickly tell me the number of lines in the file.

findstr /R /N "^" file.txt

This command will output every line with a line number in front of it but will still take a long time given a very large file. The solution to this is to take this command one step further.

findstr /R /N "^" file.txt | find /C ":"

Now the output will only be the number of lines that are contained in the file.Again, this command could be taken a step further to tell you how many lines are in the file that contain a certain string.

findstr /R /N "^.*certainString.*$" file.txt | find /c ":"

I'm sure there are many other great uses for find and findstr. If you have found one please post comment.

Related Posts:

18 Responses to “Count number of lines in a file using DOS”

  1. Ken Happich Says:

    I find this to be a very interesting use of findstr and like your website. I recently ran into an issue/limitation with findstr that I did not know existed.

    I was trying to find a pattern at a particular column location in the file and it might have been at column position aprox 140. I used the following command

    findstr ^…………………………………………………..pattern *.txt>testOutput.txt

    In my case there were serveral more periods (single charter wildcard) and I got the error message search string too long.

    Is there anyway with findstr to find a pattern at a unique location in a very long line when the pattern is closer to the end of the line?

  2. Brett Says:

    I think the below searches might help you if I understand the problem correctly.

    The below line will find "pattern" anywhere in a line.
    findstr /R ".*pattern.*" afile.txt

    The below line will find "pattern" after 140 characters.
    findstr /r "^(.*){140}pattern.*$" afile.txt

  3. iNFiNiTyLoOp Says:

    find /c/v "" afile.txt
    Is the way I figured out how to do it.

  4. Rahul Babar Says:

    We can open file using dos editor which will show the line numbers you are at.
    No need to remember special dos command.
    Simply put dos command like

    >edit file.txt

    Thanks,
    Rahul

  5. Faraz Says:

    Liked your line count command line.
    findstr /R /N "^" file.txt | find /C ":"

    I want that if I get 44 lines count from the text file then I do not want to do any thing but if it is greater or less than 44 then Exit Batch file.

    Can you help me please.

    Thanks

  6. Brett Says:

    @Rahul: that will not work on 64 bit versions of windows

    @Faraz: You can do that in a batch file with something like the following. Just change the name of yourfile.txt and possibly the temp file (save_temp_count.txt):

    @echo off
    
    :: Get the count and save it in a temp file
    findstr /R /N "^" yourfile.txt | find /C ":" > save_temp_count.txt
    
    :: Get the contents of the temp file and save it to the %count% variable
    set /p count=<save_temp_count.txt
    
    :: If greater or less than 44 (not 44) stop batch file
    if NOT %count%==44 (
    	GOTO END
    )
    
    :: count is 44
    echo "count is 44"
    
    :END
    
  7. Faraz Says:

    Brett
    Thank you for your prompt reply.
    I tested the code, batch file stuck at this below line.
    set /p count=

    Thanks

  8. Brett Says:

    @Faraz: Sorry, the code suffered from a bad paste. I have updated the above code.

    Let me know if it works for you.

  9. Faraz Says:

    Brett: Brilliant!!! It worked Great. Thank you very much, God bless you & your family.

  10. Moesjamarra Says:

    Works perfect! Thx a lot!

  11. Nogs Says:

    Hi,
    thanks a lot for your post, I still have a question:
    I want to use this line and store the count result in a variable, can anyone help?

    Thanks.

  12. Nogs Says:

    sorry, I read more carefully and this post already have what I needed.
    thanks it helped a lot!

  13. Faraz Says:

    Hi Brett,

    This below dos code when I run in batch file it Rename all files in Numeric sequence like 1.jpg, 2.jpg, 3.jpg, 4.jpg……. one problem with this code. suppose we have 99 files, When it rename the last file 99.jpg then at the end it again rename 9.jpg to last file like 100.jpg do u have a clue to fix it so it don't do that or have better dos batch file code to do this task.
    ===================================================

    @echo off
    set /a cnt=1
    for %%a in (*.jpg) do call :PROCESS "%%a"
    goto :EOF
    :PROCESS
    ren %1 %cnt%.jpg
    set /a cnt+=1

    ===================================================

    Thanks

  14. Tom Says:

    Here's another example that i found useful:

    type *.txt | findstr /R /N "^" | find /C ":"

    good for use to count total lines of all the files.

  15. mark Says:

    awesome, helped on my uni tutorial, tears since I used dos as mature student
    top stuff =)

  16. FractalSpace Says:

    Nice though, but Google brought me here on a different search. How to display current line number in a batch file (for debugging) ?

  17. john Says:

    Hello,

    I have a batch file that counts the number of lines in each text file inside a directory, and then prepends the total number of lines in each file to the beginning of each file in the directory. It looks like this:

    @echo off
    setlocal enabledelayedexpansion
    cd /d "C:\Users\John\Desktop\Testing\AllFiles"
    for /f "delims=" %%F in ('dir /b *.txt') do (
       for /f "delims=:" %%N in ('findstr /N /R "^" "%%~dpnxF"') do set lines=%%N
    
          echo There are !lines! lines in this file > "%%~dpnxF.temp"
          type "%%~dpnxF">>"%%~dpnxF.temp"
          del "%%~dpnxF"
          echo "%%~dpnxF.temp" "%%~nxF"
          ren "%%~dpnxF.temp" "%%~nxF"
    
          )

    The batch file above works great for small text files in the directory, however, it takes a long time to read the large text files in the directory.

    As you stated in your blog, using the code

    findstr /R /N "^" file.txt

    Does not work well for counting the lines in large text files. So, i added the following code:

    findstr /R /N "^" file.txt | find /C ":"

    from your blog into the batch file, and now it looks like this:

    @echo off
    setlocal enabledelayedexpansion
    cd /d "C:\Users\John\Desktop\Testing\AllFiles"
    for /f "delims=" %%F in ('dir /b *.txt') do (
       for /f "delims=:" %%N in ('findstr /N /R "^" "%%~dpnxF" | find /C ":"') do set lines=%%N
    
          echo There are !lines! lines in this file > "%%~dpnxF.temp"
          type "%%~dpnxF">>"%%~dpnxF.temp"
          del "%%~dpnxF"
          echo "%%~dpnxF.temp" "%%~nxF"
          ren "%%~dpnxF.temp" "%%~nxF"
    
          )

    But, when i run the modified code, i get the following error:

    ": was unexpected at this time"

    Do you know what i am doing wrong?

    Thanks in advance :)

  18. Dave Shibli Says:

    I had a need to number a text file before bulk inserting into a database so I could keep the lines in order.
    findstr /R /N "^" file.txt >> newfile.txt saved my life…Thanks much.
    I wrapped it in a command shell and loaded the file names from SQL and indexed nearly 5,000 text files for a forensic analysis.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.