Powershell - 测量 .tmp 文件的大小(无法找到 属性 'Length')
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
此脚本适用于 "normal" 个目录和文件,但如果目录仅包含 .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 下载程序创建的文件的目录的递归大小。
问题是 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
开关。
我正在尝试测量当前使用 BITS 下载的 ccmcache 目录的递归大小。
我正在使用以下 Powershell 脚本来测量目录的递归大小。
(Get-ChildItem $downloadPath -recurse | Measure-Object -property Length -sum).Sum
此脚本适用于 "normal" 个目录和文件,但如果目录仅包含 .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 下载程序创建的文件的目录的递归大小。
问题是 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
开关。