[英]Sum parameters got from Powershell
I am creating a script that sums up the sizes of every file under directory red line by line from a file. 我正在创建一个脚本,从文件中逐行总结目录下红色下每个文件的大小。
Right now the recursive search for files is working and I get a sum for sizes for each line of the source file but I am not sure on how to add all those values together. 现在递归搜索文件正在工作,我得到源文件的每一行的大小的总和,但我不知道如何将所有这些值添加到一起。
What I have right now is: 我现在拥有的是:
#Read each line of a file for a directory
foreach($line in Get-Content .\file.txt) {
#Prints the current line path
echo $line
#Prints a count of the files under that path and a sum of all files length
Get-ChildItem -Recurse $line | Measure-Object -Property Length -Sum
}
The output of this scripts looks like: 此脚本的输出如下所示:
T:/folder_1/folder_2/2018/12/6/A
Count : 30
Average :
Sum : 522382636
Maximum :
Minimum :
Property : Length
T:/folder_1/folder_2/2018/12/6/B
Count : 2
Average :
Sum : 2835134
Maximum :
Minimum :
Property : Length
How can I get the sum of every Sum output for every folder, ie, the sum of all .Sum
property values? 如何获得每个文件夹的每个Sum输出的总和,即所有.Sum
属性值的总和?
Combining the suggestions by notjustme and Ansgar Wiechers : 结合notjustme和Ansgar Wiechers的建议:
Get-Content .\file.txt | ForEach-Object -ov results {
# Calculate the total size for the path at hand, and
# output the result as a custom object.
[pscustomobject] @ {
Path = $_
Length = (Get-ChildItem -Recurse $_ | Measure-Object -Property Length -Sum).Sum
}
} | Measure-Object -Property Length -Sum | Select-Object -ExpandProperty Sum
# Use $results to access the per-path values.
Note how an outer Measure-Object
is used to sum the results of the per-path Measure-Object
results. 请注意外部Measure-Object
如何用于对每个路径Measure-Object
结果的结果求和。
If you don't need to store the per-path results and just want the overall sum , the solution becomes much simpler, as Ansgar observes: 如果您不需要存储每路径结果并且只需要总体总和 ,则解决方案变得更加简单,正如Ansgar所说:
(Get-ChildItem -LiteralPath (Get-Content .\file.txt) -Recurse |
Measure-Object -Property Length -Sum).Sum
Note how the array of lines output by Get-Content
is directly passed to -LiteralPath
, which is supported, because both -Path
and -LiteralPath
are defined as [string[]]
(string arrays ). 请注意Get-Content
输出的行数组如何直接传递给支持的-LiteralPath
,因为-Path
和-LiteralPath
都定义为[string[]]
(字符串数组 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.