[英]Measure-Object : The property “length” cannot be found in the input for any objects
[英]Powershell - Measure size of .tmp files (The property 'Length' cannot be found)
我正在尝试测量当前正在使用BITS下载的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
如何测量仅包含BITS下载.tmp
创建的.tmp
文件的目录的递归大小。
问题在于,BITS .tmp
文件是隐藏的,而Get-ChildItem
默认仅列出可见文件。
要测量整个目录(包括隐藏文件)的-Hidden
必须传递-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
开关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.