简体   繁体   中英

Process directory statistics recursively using Powershell

I am currently using a script I wrote to use a Logparser query against a directory recursively to produce the following output:

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

I would like to reproduce this using powershell in an attempt to make collecting this information more portable. (Not needing to install / copy logparser)

I searched and attempted to manipulate what examples I have found but can't quite get the structure down.

Here is the closest I have gotten:

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

This returns:

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

Any suggestions? I would prefer to stick to a "one line" command if possible.

Measure-Object仅执行其所要执行的操作,因此,如果仅使用-Sum ,则只会得到Sum。

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

Get-ChildItem is pretty slow; what about using RoboCopy to list the folder contents?

$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

Which goes to a one-liner:

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

Robocopy options are:

/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)

Then the ForEach splits the output to drop the filename and robocopy status and just keep the size, and measure-object calculates the results, and format-table turns it into a nicer looking table output.

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