• How can I count the number of lines/entries in a file in a folder?

    Home » Forums » AskWoody support » Windows » Windows 10 » Questions: Win10 » How can I count the number of lines/entries in a file in a folder?


    MODS:  If this doesn’t belong here, please move it, but this is the only place I could find for this question.

    Does anyone know if there is an easy way (app?) to count the number of lines in each file in a folder and get it printed out,  I tried File Explorer, but it did not seem to have an entry that would allow this information to be listed along with the other file attributes.  Any insight that anyone would care to provide will be greatly appreciated.  Thanks.

    Ron M

    Viewing 11 reply threads
    • #2404595

      See How to count the total number of lines in the file in PowerShell?
      For example:
      1) [WindowsKey]+X and pick Windows PowerShell to run it. (or find it on the Start menu)
      2) Type in: (Get-Content C:\Windows\WindowsUpdate.log).Length
      3) See line count printed: 5

      PowerShell also supports directory searches with wildcards…

      Type Get-Help about_Wildcards to get started.

      HP Compaq 6000 Pro SFF PC / Windows 10 Pro / 22H2
      Intel®Core™2 “Wolfdale” E8400 3.0 GHz / 8.00 GB
    • #2404606

      These advice is for users of Windows, macOS and Linux.

      Those who are familiar with Linux and other Unix-derivated operating systems, such as macOS/OS X, for example using a Mac or a Linux PC, and also those who are not familiar but have such computers — or have installed a Linux emulation application for Windows 7 and up through 10, for now, such as “Cygwin” (*) — if they follow a few easy instructions, can install a text editor called “vi”, if they don’t have it installed already by default (test this by launching “Terminal” and issuing the line-command “vi” (without quotes) to see if the application opens or not). I you don’t have it installed, get it using “homebrew”, if you have a Mac, and from wherever you get applications if you use Linux, and then open the file with the text in question with vi, then issue the vi command:


      to get the total lines, as well as the number of the line and of the column in the line of text where the cursor happens to be, to appear at the bottom of the screen as in this example, where “to.avoid. etc” is a file in my Mac’s desktop:

      “./Desktop/to.avoid.computer.eye.fatigue.txt” line 2 of 22 –9%– col 10


      To get there, first open the vi application by simply entering the line command:

      vi your file_name

      (If the name has blanks in it enter the name as “file name”, where your actual file might be called anything but this, of course.)

      then, when the file opens:


      If you are done, then:


      to quit vi.

      At that point you may end the Terminal session with:


      And you are all done.

      To find out more  about vi, there is a manual available using the command

      man vi

      The application vi is my favorite for editing .txt (ASCII) files and for creating ASCII files. There is practically no limit as to how large the file can be and it is also very, very fast at opening and editing even the largest files, with millions of lines of text, at least when using a computer that is not decades old.

      And there is also Powershell for Macs and for several Linux distributions, but I have  not used them, so I do not know if they have the capability already explained here by others for counting file lines in Windows PCs.

      (*) About how to get and install with a few examples of some basic use of Cygwin in Windows 10, particularly how to access files created with Cygwin from Windows and vice versa:


      Ex-Windows user (Win. 98, XP, 7); since mid-2017 using also macOS. Presently on Monterey 12.15 & sometimes running also Linux (Mint).

      MacBook Pro circa mid-2015, 15" display, with 16GB 1600 GHz DDR3 RAM, 1 TB SSD, a Haswell architecture Intel CPU with 4 Cores and 8 Threads model i7-4870HQ @ 2.50GHz.
      Intel Iris Pro GPU with Built-in Bus, VRAM 1.5 GB, Display 2880 x 1800 Retina, 24-Bit color.
      macOS Monterey; browsers: Waterfox "Current", Vivaldi and (now and then) Chrome; security apps. Intego AV

    • #2404613

      EyesOnWindows and OscarCP, thanks for the discussion on the use of  Powershell to count the number of lines in a file inside a folder.  Guys, I have never had the opportunity, probably a requirement, to use Powershell, so I am unclear exactly how it works.  For example, EyesOnWindows, in your instructions:

      “For example:
      1) [WindowsKey]+X and pick Windows PowerShell to run it. (or find it on the Start menu)
      2) Type in: (Get-Content C:\Windows\WindowsUpdate.log).Length  "

      I am not sure what you mean in the first line by “pick Windows PowerShell to run it.”  I know how to use the [WindowsKey].  In 2) where you show a file location, do I assume that “WindowsUpdate.log” is the file name I would type in.  For example in my case it would be “All 2021 CSV Files.csv”.  Does this mean that I would have to do this for every file in the folder?

      Your second example shows how to do it for all of the files in a folder, so that answers that question.  I am still unclear about the syntax of the line:

      Get-ChildItem C:\Windows -Filter “*.log” -Name | ForEach-Object{(Get-Content $_).Length}

      Does this line print out the filename as well as the number of lines
      in the file?

      I am not knowledgeable regarding the syntax of these lines for
      PowerShell, so I find I have to understand what each component of the command
      line means in terms of what it is doing.
      Ron M

    • #2404614

      EyesOnWindows, I tried your first example and it did not work.  I must have forgotten something as it didn’t give me an answer.  I will try again later as I have a previous commitment to deal with.

      Ron M

    • #2404670

      To count the number of lines of text in all text files in a folder by file:

      Run the cmd.exe command prompt, then type:
      cd /folder
      find /c /v  “”  *.txt

      Replace “folder” with the path to the folder you wish to query.

      Windows 10 22H2 desktops & laptops on Dell, HP, ASUS; No servers, no domain.

      • #2404689

        You must use a pair of plain double-quotes "" for this to work. For example you can do this without first having to CD to the directory:

        find /c /v "" C:\Windows\*.log

        Which then prints:

        ---------- C:\WINDOWS\COMSETUP.LOG: 9

        ---------- C:\WINDOWS\DPINST.LOG: 42

        ---------- C:\WINDOWS\DTCINSTALL.LOG: 5

        ---------- C:\WINDOWS\PFRO.LOG: 2300

        ---------- C:\WINDOWS\SETUPERR.LOG: 0

        ---------- C:\WINDOWS\WINDOWSUPDATE.LOG: 5

        Strangely PowerShell reports twice as many lines, 4603, for C:\WINDOWS\PFRO.LOG using various methods. There are actually 2301 lines, the last line is blank.

        HP Compaq 6000 Pro SFF PC / Windows 10 Pro / 22H2
        Intel®Core™2 “Wolfdale” E8400 3.0 GHz / 8.00 GB
        • #2404725

          The C:\Windows\PFRO.log file is written in Microsoft’s Unicode format, which means 16-bit characters. When treated as 8-bit characters, carriage-return and new-line characters are seen as separated by a null character hence double the line count.

          Microsoft’s Unicode format uses UTF-16, little endian byte order. If you save the file as a new file with “UTF-16 LE” encoding using Notepad, a 2-byte BOM is added to the beginning of the new file for which PowerShell will then correctly report the line count.

          As an alternative you can add the “-Encoding Unicode” option to the “Get-Content” cmdlet for any such files to get the correct line count.

          HP Compaq 6000 Pro SFF PC / Windows 10 Pro / 22H2
          Intel®Core™2 “Wolfdale” E8400 3.0 GHz / 8.00 GB
    • #2404671

      You should replace Windowsupdate.log with the file name you want if you are doing this for one file.

      The second example where there is “*.log” will process all .log files in the specified folder.  You may specify any sort of substitution for specific file types. If you don’t specify any filter the Get-ChildItem command will return all files in the folder (called a container in PS). When you run the second example use the CD command to make sure you are in the folder you want.

      Also, there is extensive help available in PowerShell. Use Get-Help Get-ChildItem to see the help for that command.

      There is also extensive documentation for PowerShell at PowerShell Documentation – PowerShell | Microsoft Docs


    • #2404694

      While holding the “Windows” key down press the letter “X” key. Click “Windows PowerShell” on the menu that pops up. This causes “PowerShell” to start running.

      The second example I gave only works if you do a CD C:Windows in PowerShell first. (Sorry.) It only prints the line counts.

      To print the filenames as well type the following all on one line as shown in this snapshot:


      HP Compaq 6000 Pro SFF PC / Windows 10 Pro / 22H2
      Intel®Core™2 “Wolfdale” E8400 3.0 GHz / 8.00 GB
      • #2404719

        Wordfence gave me a “HTTP response code 403” error when I tried to enter the PowerShell example command line before. So I posted a snapshot instead. Then I gave my ip address to customer support who says to try again now.
        So here it is again swaddled in backticks:

        Get-ChildItem -Path C:\Windows\*.log | Select-Object -Property FullName,@{Name="LineCount";Expression={@(Get-Content -Path $_.FullName).Length}}

        HP Compaq 6000 Pro SFF PC / Windows 10 Pro / 22H2
        Intel®Core™2 “Wolfdale” E8400 3.0 GHz / 8.00 GB
    • #2404750

      Ron M,

      If this topic/thread is related to your other topic, “Saving multiple CSV files a single CSV file“, then oldfry’s answer is simple, quick, and exactly what you want. However, note oldfry is assuming you’re searching txt files, so you’ll have to specify you want to search csv files instead of txt files.

      IOW, if your command prompt’s current working directory (cwd) is where your csv files are, then issuing the command:

      find /c /v "" *.csv

      will list the number of lines in each csv file. As EyesOnWindows pointed out, if the cwd is not where the csv files are, simply adjust the command to prepend the path before *.csv.


    • #2404793

      The find /c /v “” *.csv command works great in a Command Prompt for .txt, .log, and .csv file formats.  I tried other formats which returns wrong answer such as a .dotx file having 32 lines being reported with 3304 lines.
      Is there a list of file formats that this line counting command will work on accurately?


      HTH, Dana:))

    • #2404806

      The /c /v command counts all the lines in a file, not just lines that contain text characters.

        BTW,  a line is anything that ends with a CR + LF combo (0x0D 0x0A)

      The line count matches for the .txt, .log and .csv file formats because they only contain standard text characters.

      Other formats (such as .docx files) report more lines than just the text you see when you open them is because they actually contain extra lines containing the special codes used to format the info they contain (i.e. font size, font color, bold, underlined, margins, padding, tables, lists, etc., etc.)

      If you right click such a file, select “open with“, and use notepad to open it, you can see all those extra lines (they’ll look like gobbledygook since they’re special codes only the intended program can understand.)


      The line count for files that contain only “standard text characters” should always match. The file format (defined by it’s extension) doesn’t always matter because it’s possible to save a text-only file with any extension or even no extension.

      Some other common file types that normally contain only text are:

        .bat, .css, .html, .ini, .js, .ps1, .reg, .srt, .svg, .xml, .xul

      The line count for files types that include special formatting of the info they contain, will not match.

      1 user thanked author for this post.
    • #2404838


      Since you’re new to PowerShell I generated the following commented code to help you along. You’ll notice that it is not a one liner. One liners are ok for experienced users but not very helpful for newbies.

      Clear-Host   #Clear the console display.
      $TotalLen = 0 #Setup a counter for total length of all files.
      #Retrieve a list of .log files
      $Files = Get-ChildItem C:\Windows -Filter “*.log” -File
      ForEach ($File in $Files) {
        #Test for existing file that is empty.
        Try {
          $FLen = (Get-Content -path $($File.FullName)).Length
        Catch {$FLen = 0}
       #Output each files result
       "{0,9:#,##0} {1}" -f $FLen, $($File.FullName)
       $TotalLen += $FLen  #Add file length to total
      #Output total length of all files.
      "{0,9:#,##0} Total Length of all files." -f $TotalLen

      Instructions to run the above code:
      Copy the code into a file called Get-FileLineCount.ps1 and save
      Open PowerShell.exe as Administrator (only necessary for first run)
      Type: set-executionpolicy RemoteSigned -force {enter}
      Note: the above command only needs to be entered once.
      Type: d:\path\Get-FileLineCount.ps1 {enter}
      Sample Output:

              9 C:\Windows\comsetup.log
             79 C:\Windows\DDACLSys.log
            354 C:\Windows\DPINST.LOG
              5 C:\Windows\DtcInstall.log
          7,267 C:\Windows\PFRO.log
          3,536 C:\Windows\setupact.log
              0 C:\Windows\setuperr.log
              5 C:\Windows\WindowsUpdate.log
         11,255 Total Length of all files.


      May the Forces of good computing be with you!


      PowerShell & VBA Rule!
      Computer Specs

    • #2514350

      EyesOnWindows, your solution works well for my scenario. Need help writing the output of the script into a CSV file.

      The code shared by you on <span class=”bbp-post-date”>December 5, 2021 at 9:30 pm </span>#2404719


      The output is-

      FullName                                           LineCount
      ——–                                                 ———
      C:\Windows\ANCD065.log            296
      C:\Windows\ANCE065.log           296
      C:\Windows\ANCF065.log          296
      C:\Windows\ANCG065.log         296

      How to write the output in csv file?


    Viewing 11 reply threads
    Reply To: How can I count the number of lines/entries in a file in a folder?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: