45

On a Windows 7 machine, I'd like a quick way to determine the number of lines in all of the files in a directory tree, for files of a given extension. Is there any good out of the box way to do this?

3
  • 1
    [Do you know a similar program for wc (unix word count command) on Windows?][1] [1]: stackoverflow.com/questions/247234/…
    – sinping
    Oct 12, 2011 at 17:50
  • I'm familiar with wc, I wish there was a W7 equivellant. Oct 12, 2011 at 18:17
  • @ (quillbreaker) There is. The entire Linux/Unix command line toolkit is available for ALL windows based platforms. gnuwin32.sourceforge.net
    – shawty
    Apr 20, 2018 at 18:51

5 Answers 5

82

You can use Measure-Object in powershell like this:

PS D:\temp> dir -Recurse *.txt | Get-Content | Measure-Object -Line

Which will return the following:

Lines Words Characters Property
----- ----- ---------- --------
  168
8
  • 6
    While your answer is helpful and accepted by the OP, it doesn't correctly answer OP's question. The problem is in the first command in your solution - dir -Recurse *.txt will only process directories whose names match *.txt, while outputting all the files that it finds in those directories and their subtrees. But the OP wanted to process all directories while outputting only the files that match *.txt. To do that, you would use dir -Recurse -Include *.txt.
    – Cinnam
    May 26, 2016 at 2:19
  • 3
    This is incorrect, the windows help on Get-ChildItem (which dir is an alias of) states that Get-ChildItem –Path "*.txt" -Recurse is all that is needed to get "all of the .txt files in the current directory and its subdirectories." link: technet.microsoft.com/library/hh849800.aspx Aug 27, 2016 at 3:52
  • @AndrewLeap I just tested it again - on Win8 with PowerShell 4.0 it works as you say. However on Win7 with PowerShell 2.0 it has the problem I described. dir -Recurse -Include *.txt seems to have the desired results on both of these versions.
    – Cinnam
    Sep 1, 2016 at 17:08
  • How would someone use this to check in BOTH *.txt AND *.java (combined total)? Oct 3, 2017 at 15:09
  • 1
    @Jeremy, forget to mention that (dir -Recurse "*.java", (([Environment]::GetFolderPath("Desktop"))+"\*.txt") | Get-Content | Measure-Object -Line).Lines to get multiple paths or extensions. Just give the dir an array.
    – Ste
    Mar 23, 2021 at 17:02
11

To expand on maweeras answer above (sorry not enough rep to comment), you can search for multiple file extensions by passing a comma-delimited array to -Include.

So for example:

dir -Recurse -Include *.ts,*.tsx -Exclude *node_modules* | Get-Content | Measure-Object -Line
1
  • Only works with one extension, soon as I add another I get 0 lines
    – jjxtra
    Jan 3, 2022 at 20:45
4

The following works in cmd:

for %f in (*.TXT) do find /v /c "" "%f"

Or in a [.bat]:

for %%f in (*.TXT) do find /v /c "" "%%f"
1

One way would be with cloc

  1. Download the release exe

  2. Add the folder its in to the path e.g. in the CLI: set path=%path%C:\Folder\Path\Cloc\Is\In

  3. Go to the top directory you want to analyze

  4. Run the following commands

For analyzing each subfolder separately

for /D %I in (.\*) do cd %I && cloc . && cd ..

enter image description here

For getting a joint report of all subfolders

cloc .

enter image description here

0

Recursively get all line for your *.ps1;*.cs;*.js;*.txt files

  • In the current folder .:
for /f tokens^=* %i in ('where/r . *.ps1;*.cs;*.js;*.txt^|find/v /c  ""')do @echo\ %i
  • In the current drive \:
for /f tokens^=* %i in ('where/r \ *.ps1;*.cs;*.js;*.txt^|find/v /c  ""')do @echo\ %i
  • In the specified folder C:\User\Sources:
for /f tokens^=* %i in ('where/r "C:\User\Sources" *.ps1;*.cs;*.js;*.txt^|find/v /c  ""')do @echo\ %i

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .