我正在尝试测量当前正在用位下载的ccmcache目录的递归大小。
我使用下面的Powershell脚本来度量目录的递归大小。
(Get-ChildItem $downloadPath -recurse | Measure-Object -property Length -sum).Sum此脚本适用于“普通”目录和文件,但如果该目录仅包含.tmp文件,则将出现以下错误。
Measure-Object : The property "Length" cannot be found in the input for any objects.
At line:1 char:27
+ (Get-ChildItem -Recurse | Measure-Object -Property Length -Sum).Sum
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand如何度量仅包含由位下载器创建的.tmp文件的目录的递归大小。
发布于 2016-02-19 10:36:08
问题是比特.tmp文件是隐藏的,Get-ChildItem默认只列出可见文件。
要度量整个目录(包括隐藏文件)的大小,必须传递-Hidden开关。
(Get-ChildItem $downloadPath -Recurse -Hidden | Measure-Object -property Length -sum).Sum但这将只计算隐藏文件,不包括所有可见文件。因此,为了计数所有文件,必须添加隐藏和可见和的结果:
[long](Get-ChildItem $downloadPath -Recurse -Hidden | Measure-Object -property length -sum -ErrorAction SilentlyContinue).Sum + [long](Get-ChildItem $downloadPath -Recurse | Measure-Object -property length -sum -ErrorAction SilentlyContinue).Sum 如果不存在隐藏文件或可见文件,则会发生错误。正因为如此,-ErrorAction SilentlyContinue交换机才被包括在内。
https://stackoverflow.com/questions/35501891
复制相似问题