Count number of lines in a file using DOS

by Brett on February 25, 2007

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.

  • Ken Happich

    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?

  • 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

  • iNFiNiTyLoOp

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

  • Rahul Babar

    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


  • Faraz

    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.


  • @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 (
    :: count is 44
    echo "count is 44"
  • Faraz

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


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

    Let me know if it works for you.

  • Faraz

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

  • Moesjamarra

    Works perfect! Thx a lot!

  • Nogs

    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?


  • Nogs

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

  • Faraz

    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
    ren %1 %cnt%.jpg
    set /a cnt+=1



  • Tom

    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.

  • mark

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

  • FractalSpace

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

  • john


    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 :)

  • 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.

  • Jadh

    This does not work for really big files such as 10 million row. Do you know how to make it work for big files?

  • @Jadh, I just tried this on a file that had 100 million lines (I added 90 million to your number). I used the following command and it worked perfectly.

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

    It took a few minutes for it to go through the file but it eventually output 100,000,000. I ran this on Windows 7 64 bit with 8GB memory.

  • Jadh

    Yup you are right. I was reconciling different records…Thanks for the quick resposne.

  • Robert

    Using Brett’s findstr command string, I wrote the following to poll the connection status of my Android device over ADB. The script keeps looping until a device is connected.

    echo OFF


    adb devices > adb_devices.txt
    findstr /R /N “^” adb_devices.txt | find /C “:” > linecount.txt
    set /p count=<linecount.txt
    if %count% LEQ 2 GOTO START


  • ss

    Hi Brett

    I used findstr /R /N “^” test.xml
    I renamed test.xml to test.xml1 then also the command worked.
    I was expecting command will fail since test.xml is no longer there, but it still worked. Can you please tell me the reason.

  • abha

    I want to delete the last 2 lines of the file in DOS.
    I do not want to read each line and copy it to a new file. As the file contains thousands of record and it is taking too much time.
    Basically i want to copy all the contents of a file except the last 2 lines.
    I also have the total number of lines..Just if it makes the task easy.
    Please help……

  • Anupam

    To count the number of lines in a file why would be do
    findstr /R /N “^” file.txt | find /C “:”
    Instead one can use
    find /C /V “” file.txt
    Also for counting the occurance of particular string in a file what is wrong in using find /C “StringToSearch” file.txt ?


  • Anupam

    Moreover findstr /R /N “^” file.txt | find /C “:” gives wrong results if there are : in a file which is very common for log files with timestamps.

  • satish

    i have executed sql file and generated logfile. it has around 80000 lines. But it has 2 empty lines between every two lines.
    i have used
    findstr /R /N “^.*certainString.*$” filename.txt
    but i am get wrong output. mybe because of gap between lines.
    so can you give me a fix for this ? and i need to use this command frequently.

  • satish

    Hi Brett,
    Thanks so much for your quick reply. following statement has fulfilled my requirement.

    findstr  "string"  filename  | find  /c":"

    The blank lines are ignored when it is searching for the “string” .
    thanks for your post.and Happy new year………

  • Partha

    Hey, your post is great…I need a help with a script …to find and replace a string in a xml file..preferablly using regular expression.
    Your code to find the number of occurance works great,..I need script to find and replace a string…

    Can you help ..


  • Steve

    This…. is awesome. Thanks very much for posting. Just used this on a 4.6m line file (thank goodness)

  • Maria

    i’m greatful if anyody can help me with this issue. i want to get a variable in the 1st line of a txt file and store it in a new variable
    yymmdd 010 xxx
    I want to open the file, store the seq-no which is 010 to a variable and close the file

  • Kim


    I am trying to write a batch file to be run in a dos command prompt on XP. I am trying to get a listing of files in a specific path that follow a certain naming convention. I need to copy and rename each file instance to a static name and drop it to a transmission folder. Since it may take a little while for the file to go in the transmission folder, I need to check before I copy the next file over so that I don’t overlay the previous file. I am not able to use SLEEP or TIMEOUT since I don’t have the extra toolkit installed. I try to just continually loop back to a START section until the file is sent. I noticed that if I passed the %%x value set in the for loop that if I loop back to the START section a couple of times, it seems to loose its value and it is set to nothing. So I tried to set a variable to hold the value. I seem to be having issues with the variable not being set correctly or not cleared. Originally it kept on referencing the first file but now it doesn’t seem to be set at all. The ECHO displays the correct the value but the filename variable is empty still. Does anyone know a better way of doing this? Thanks in advance for your help as I have already wasted a whole day on this!



        @ECHO "At the start of the loop"
        @for %%x in (C:\OUTBOUND\customer_file*) do (
        @ECHO "In the loop"
        @ECHO "loop value ="
        @ECHO %%x
        SET filename=%%x
        @ECHO "filename ="
        @ECHO %filename%
        archivedatafile --sourcefile="%filename%" --archivefolder="..\archivedata\customer" --retentiondays=0
        IF EXIST l:\OutputFile (
    	@ping -n 1 -w 30000
        ) ELSE (
    	COPY %filename% l:\OutputFile /Y
        GOTO END
        @echo off
        @ECHO *************************************************************
        @ECHO *                      !!ERROR!!                            *
        @ECHO *************************************************************
        SET filename=
  • Hi All!

    The count will be incorrect if there are null characters in the input to the find command because find outputs them as newlines.

    The program below uses only findstr (and some chicanery). It’s fast on large files, copes with extremely long lines, and doesn’t mind if the input file has Windows or Unix line endings.

    @echo off & setlocal enableextensions
    (set lf=^
    call :LFcount lc "%~1"
    echo(file "%~1" has %lc% lines
    endlocal & goto :EOF
    setlocal enabledelayedexpansion
    findstr /mv "!lf!" "%~2" >nul && (
    for /f delims^=: %%n in ('
    cmd /v:on /c findstr /nv "^!lf^!" "%~2" 2^>nul
    ') do set lastline=%%n) || (for /f delims^=: %%n in ('
    (findstr /n "^" "%~2" ^& echo(#^) ^| findstr /bn # 2^>nul
    ') do set /a lastline=%%n-1)
    endlocal & set "%1=%lastline%" & exit /b 0

    For more info, read this blog post:

  • @Maria

    Try this:

    @echo off & setlocal enableextensions
    for /f "usebackq tokens=2" %%s in ("aaa.txt") do (
    set seqno=%%s
    goto break
    echo(seq no is %seqno%
    endlocal & exit /b 0

    Copy & Paste the above into a file and call it seqno.cmd. Open a Command Prompt in the folder where you saved the file, and type


    on its own and you should see the result. Let me know if you have any more problems.

  • AlSalman


    Thanks all for your input, that was helpful to me…

    I used this command

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

    To find the total number of lines in all test*.txt files (3 files) but got wrong count (13 Lines)!
    Each file has 5 lines (no blank lines at the top, bottom or in the middle)….

    Using this command shows why:

    findstr /R /N "^" test*.txt

    Output is:


    It is like each last line of a file is counted as one with the first line of the next file???

    Any advise how to solve this?


  • Michael Q

    Thanks for this page. I think you should add multiple strings to this as well.

    I did the following to look for XX and YY at the same time:

    findstr /c:"XX" /c:":YY" /n file.txt | find /c ":" > temp_linecount.txt
    set /p TCOUNT=<temp_linecount.txt
    if exist temp_linecount (del temp_linecount)
    echo %TCOUNT%
  • Satish

    Hi all, I have a query.
    i have a text file with the contents as :

    These are comma separated.
    Please help me in writing a batch file that searches for “0000000030” on the 4th column and if it finds “000000000” to the corresponding 5th column then delete the entire row

  • Gaurav

    Hi Brett,

    I have below requirement. Below are two files File 1 and File 2.
    abc hello
    abc hi
    abc we
    abc changes start
    how are you?
    I am fine.
    What about you?
    I am fine.
    abc changes end
    File 2:
    xyz hi
    xyz hello
    xyz yup
    xyz changes start
    xyz changes ends

    In File 1 “abc” is a string here I want to find the count of string “abc” and then I have to add that count to the lines between abc changes start and abc changes end strings.
    In same way I have to find for File2 which has “xyz” string and in the end I have to add the total count.
    For example: File 1 will have a total count of “abc” string + count of lines between “abc changes start” and “abc changes ends” that is 3+4 =7 and File 2 will be having for xyz and changes start and changes end string 3+3 =6 and in the end total count will be 7+6=13.

    Thanks in advance.

  • Vishnu


    I have to go to last line of a log file and check a string , string will be available on many lines. I have to particularly check the string in last line . Please provide your concerns.


  • amit

    can we get a count of characters in a text file and then compare it?

  • Thank you, Brett, for taking your time posting what you’ve revealed. “The country will not forget you” (c)

  • Amit

    Thanks. very helpful website. I need help writing a DOS command to do “record count” in a file and ignore if any “new line” character in the file.
    file has | delimeter.
    Code I am using to find count:

    set filea=abc.txt
    set /a cnt=0
    for /f %%a in (‘type “%file%”^|find “” /v /c’) do set /a cnt=%%a+1
    echo %file% has %cnt% lines

    For Ex:
    L1234|John D Pinto| abc
    I will get 1 count. Thats correct.

    Now if my data will be like:
    L1234|John D
    Pinto| abc

    In 2 lines then also I want to count as 1 record however parameter is counting as 2 count as it is currently doing line count NOT record count. Any help will be appriciated.
    Thanks for reading.

  • Frank Jeppesen

    Saved my day, trying to transport a perl program from running on unix with system commands using ‘wc -l’.
    My windows rewrite was less than pretty .. and here I saw that it cound be done in one line !! 8))
    Cheers from Copenhagen, Frank

  • saravanan CM

    The command => findstr /R /N “^” file.txt is very nice to see the line.But it seems takes long time to display all the records .Is possible to display last line with line number instead of displaying all the line numbers

  • Ian

    Any way I can modify your script that you posted on May 24, 2013 at 3:04 pm
    to output the last line of the file after the count has been output ?

    I’ve tried for loop and skipping to the end – 1 but nothing is returned. I’m new to .bat file syntax so maybe I’m putting it in the wrong place. Any guidance much appreciated

  • Anirban

    hi brett..i have a requirement to count the number of lines in a file, but the count should exclude commented lines (i.e. lines which have * in position 1 or 7). actually, i was dealing with cobol programs, which is why the commented lines would appear in this position. can this be done using dos scripting?

  • Manoj


    I have a text file containing 5 million records, i need to remove the trailing spaces for each record.
    Could you help me please. I have a script which does this but it woks well only smaller files say 250 MB.
    When input file is 2 GB, output file is returned as empty ? Why so ?

    @echo off
    setlocal enableDelayedExpansion
    set MYDIR=C:\Documents and Settings\test
    set “spcs= ”
    for /l %%n in (1 1 12) do set “spcs=!spcs!!spcs!”
    findstr /n “^” “%~1″>”%~1.tmp”
    setlocal disableDelayedExpansion
    for /f “usebackq delims=” %%L in (“%~1.tmp”) do (
    set “ln=%%L”
    setlocal enableDelayedExpansion
    set “ln=!ln:*:=!”
    set /a “n=4096”
    for /l %%i in (1 1 13) do (
    if defined ln for %%n in (!n!) do (
    if “!ln:~-%%n!”==”!spcs:~-%%n!” set “ln=!ln:~0,-%%n!”
    set /a “n/=2″
    ) >”%~1”
    del “%~1.tmp” 2>nul

    move “%~1” “%MYDIR%”
    echo. 2>”%MYDIR%file_is_ready.txt”

  • Anna

    I need to Count the No : Of Records in the File without the Header Count.
    For ex: If the File Contains 20 Rows with the Header , I need the Count to be displayed as 19.

    The Below Command works fine , but the Row Count should be Minus the Header (-1)

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


  • Bill

    How would you run a command to add the sum of all lines in multiple files? I’d like to know the total amount of code in my software

Previous post:

Next post: