繁体   English   中英

Powershell-测量.tmp文件的大小(找不到属性“长度”)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM