如何在 Windows 上递归计算目录中的所有代码行?

qui*_*ker 35 windows-7

在 Windows 7 机器上,对于给定扩展名的文件,我想要一种快速的方法来确定目录树中所有文件的行数。有什么好的开箱即用的方法来做到这一点吗?

小智 62

您可以Measure-Object像这样在 powershell 中使用:

PS D:\temp> dir -Recurse *.txt | Get-Content | Measure-Object -Line
Run Code Online (Sandbox Code Playgroud)

这将返回以下内容:

Lines Words Characters Property
----- ----- ---------- --------
  168
Run Code Online (Sandbox Code Playgroud)

  • 虽然您的回答很有帮助并被 OP 接受,但它并没有正确回答 OP 的问题。问题出在您的解决方案中的第一个命令中 - `dir -Recurse *.txt` 将只处理名称与 `*.txt` 匹配的目录,同时输出它在这些目录及其子树中找到的所有文件。但是 OP 想要处理所有目录,同时只输出匹配 `*.txt` 的文件。为此,您可以使用`dir -Recurse -Include *.txt`。 (6认同)
  • 这是不正确的,关于`Get-ChildItem`(`dir` 是其别名)的Windows 帮助指出`Get-ChildItem –Path "*.txt" -Recurse` 是获取“所有当前目录及其子目录中的 .txt 文件。” 链接:https://technet.microsoft.com/library/hh849800.aspx (3认同)

小智 8

要扩展上面的 maweeras 答案(抱歉没有足够的代表来评论),您可以通过将逗号分隔的数组传递给 -Include 来搜索多个文件扩展名。

例如:

dir -Recurse -Include *.ts,*.tsx -Exclude *node_modules* | Get-Content | Measure-Object -Line
Run Code Online (Sandbox Code Playgroud)