windows 使用 Powershell 命令进行文件计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8239905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:57:34  来源:igfitidea点击:

File counting with Powershell commands

windowspowershell

提问by Elmex

How can I count all files in a specific folder (and all subfolders) with the Powershellcommand Get-ChildItem? With (Get-ChildItem <Folder> -recurse).Countalso the folders are counted and this is not that what I want. Are there other possibilities for counting files in very big folders quickly?

如何使用Powershell命令 Get-ChildItem计算特定文件夹(和所有子文件夹)中的所有文件?随着(Get-ChildItem <Folder> -recurse).Count也文件夹进行计数,这不正是我想要的。是否有其他可能性可以快速计算非常大的文件夹中的文件?

Does anybody know a short and good tutorial regarding the Windows Powerhell?

有人知道关于 Windows Powerhell 的简短而好的教程吗?

回答by Shay Levy

I would pipe the result to the Measure-Objectcmdlet. Using (...).Count can yield nothing in case there are no objects that match your criteria.

我会将结果通过管道传输到Measure-Objectcmdlet。如果没有符合您的条件的对象,则使用 (...).Count 不会产生任何结果。

 $files = Get-ChildItem <Folder> -Recurse | Where-Object {!$_.PSIsContainer} | Measure-Object
 $files.Count

In PowerShell v3 we can do the following to get files only:

在 PowerShell v3 中,我们可以执行以下操作来仅获取文件:

 Get-ChildItem <Folder> -File -Recurse

回答by jon Z

Filter for files before counting:

计数前过滤文件:

(Get-ChildItem <Folder> -recurse | where-object {-not ($_.PSIsContainer)}).Count