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