1

我正在尝试测量当前正在使用 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

如何测量仅包含.tmp由 BITS 下载器创建的文件的目录的递归大小。

4

1 回答 1

1

问题是 BITS.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开关被包括在内。

于 2016-02-19T10:36:08.630 回答