[英]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. 我正在尝试测量当前正在使用BITS下载的ccmcache目录的递归大小。
I am using the following Powershell script to measure the recursive size of the directory. 我正在使用以下Powershell脚本来测量目录的递归大小。
(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. 该脚本适用于“普通”目录和文件,但是如果目录仅包含.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
How can I measure the recursive size of a directory that only contains .tmp
files created by the BITS downloader. 如何测量仅包含BITS下载.tmp
创建的.tmp
文件的目录的递归大小。
The problem is that BITS .tmp
files are hidden and Get-ChildItem
only lists visible files by default. 问题在于,BITS .tmp
文件是隐藏的,而Get-ChildItem
默认仅列出可见文件。
To measure the size of the whole directory, including hidden files, the -Hidden
switch must be passed. 要测量整个目录(包括隐藏文件)的-Hidden
必须传递-Hidden
开关。
(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. 因此,包含了-ErrorAction SilentlyContinue
开关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.