简体   繁体   中英

Powershell - Measure size of .tmp files (The property 'Length' cannot be found)

I am trying to measure the recursive size of a ccmcache directory that is currently being downloaded with BITS.

I am using the following Powershell script to measure the recursive size of the directory.

(Get-ChildItem $downloadPath -recurse | Measure-Object -property Length -sum).Sum

This script works for "normal" directories and files, but it fails with the following error if the directory only contains .tmp files.

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

How can I measure the recursive size of a directory that only contains .tmp files created by the BITS downloader.

The problem is that BITS .tmp files are hidden and Get-ChildItem only lists visible files by default.

To measure the size of the whole directory, including hidden files, the -Hidden switch must be passed.

(Get-ChildItem $downloadPath -Recurse -Hidden | Measure-Object -property Length -sum).Sum

But this would only count hidden files, excluding all visible files. So in order to count all files, the results of the hidden sum and visible sum must be added:

[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 

If no hidden files or visible files exist an error will occur. Because of that the -ErrorAction SilentlyContinue switch is included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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