使用 Powershell 递归处理目录统计信息

Process directory statistics recursively using Powershell

我目前正在使用我编写的脚本以递归方式对目录使用 Logparser 查询以生成以下输出:

QTY     TOT KB    AVRG KB MAXM KB MINM KB
------- --------- ------- ------- -------
3173881 175101609 55      85373   0

我想使用 powershell 重现此内容,以尝试使收集此信息更加便携。 (不需要安装/复制 logparser)

我搜索并试图操纵我找到的示例,但无法完全理解结构。

这是我得到的最接近的:

Get-ChildItem -Recurse | Measure-Object -sum Length | Select-Object Count,Average,Sum

这个returns:

Count Average                                                                     Sum
----- -------                                                                     ---
44663                                                                     40861708776

有什么建议吗?如果可能的话,我宁愿坚持使用 "one line" 命令。

Measure-Object 只做它告诉的事情,所以如果你只 -Sum,你只会得到总和。

Get-ChildItem -Recurse -File | Measure-Object -Sum -Average -Maximum -Minimum -Property Length | Select Count, Average, Sum, Maximum, Minimum

Get-ChildItem 非常慢;如何使用 RoboCopy 列出文件夹内容?

$Folder = "D:\Downloads"
robocopy $Folder $Folder /S /L /BYTES /NJH /NJS /NDL /V | 
    ForEach-Object { (-split $_)[1] } | 
    Measure-Object -Maximum -Minimum -Sum -Average | 
    Format-Table

其中一条线:

robocopy $Folder $Folder /S /L /BYTES /NJH /NJS /NDL /V |%{(-split $_)[1]}|measure -a -s -ma -mi | ft

Robocopy 选项是:

/S - subdirectories (excluding empty ones)
/L - list files, don't do any copying or moving
/BYTES - show sizes in bytes, with no commas or anything
/NJH and /NJS - no header and summary lines in the output
/NDL - don't list directories
/V - verbose (do list individual files and their sizes)

然后 ForEach 拆分输出以删除文件名和 robocopy 状态并保持大小,measure-object 计算结果,format-table 将其转换为更好看的 table输出。