gpt4 book ai didi

powershell - 从Powershell获得的总和参数

转载 作者:行者123 更新时间:2023-12-03 01:02:49 24 4
gpt4 key购买 nike

我正在创建一个脚本,该脚本逐行汇总文件中目录红色下每个文件的大小。

现在,递归搜索文件正在工作,并且我得到了源文件每一行的大小总和,但是我不确定如何将所有这些值加在一起。

我现在所拥有的是:

#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
}

该脚本的输出如下所示:
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

如何获得每个文件夹的每个Sum输出的总和,即所有 .Sum属性值的总和?

最佳答案

结合notjustmeAnsgar 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.

请注意,如何使用外部 Measure-Object对每个路径的 Measure-Object结果的结果求和。

如果您不需要存储每个路径的结果,而 只需要总体和,则解决方案将变得更加简单,如Ansgar所说:
(Get-ChildItem -LiteralPath (Get-Content .\file.txt) -Recurse |
Measure-Object -Property Length -Sum).Sum

请注意 Get-Content输出的行数组如何直接传递到 -LiteralPath,因为 -Path-LiteralPath都被定义为 [string[]](字符串数组),因此受支持。

关于powershell - 从Powershell获得的总和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53672335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com