簡體   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