深入了解PowerShell脚本目录列表并提供大小

时间:2019-06-26 17:13:23

标签: powershell

我的文件夹结构是:

P:\12345
P:\12345\001
P:\12345\001\folder
P:\12345\002
P:\12345\002\folder
P:\67890
P:\67890\001
P:\67890\002

我的最终目标是创建一个脚本,该脚本将为我提供csv到第二级的路径,并提供此级及以下级别的大小。我目前正在使用两种不同的脚本进行这项工作。

输出:

P:\12345\001; 1003
P:\12345\002; 631
P:\67890\001; 594
P:\67890\002; 467

这可以通过一个脚本完成吗?

$startFolder = "P:\01520"
$output = "c:\temp\01520.csv"

$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
$results = @()
foreach ($i in $colItems)
    {
        $i.FullName
        $subFolderItems = (Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum)
        $results += '"' +$i.FullName + '"' + ";" + "{0:N2}" -f ($subFolderItems.sum / 1MB)
    }
$results > $output

============================

Get-ChildItem \*\* | % { if ($_.Attributes -eq "Directory") { Out-File C:\Temp\DirList.txt -append -inputobject $_.FullName } }

2 个答案:

答案 0 :(得分:0)

这是一种实现方法。它会构建一个[PSCustomObject],其中包含来自各种目录树的数据,并将其输出到屏幕上显示并保存到CSV文件的$Results集合中。

# fake reading in a list of dirs
#    in real life, use Get-Content
$DirList = @'
C:\Temp\Logitech
C:\Temp\BackupSets - Copy
C:\Temp\chocolatey
C:\Temp\zzz - Copy
'@ -split [System.Environment]::NewLine

$Results = foreach ($DL_Item in $DirList)
    {
    $SubDirList = Get-ChildItem -LiteralPath $DL_Item -Directory
    foreach ($SDL_Item in $SubDirList)
        {
        $DirTreeInfo = Get-ChildItem -LiteralPath $SDL_Item.FullName -Recurse -File
        $Size_MB = [math]::Round(($DirTreeInfo |
            Measure-Object -Property Length -Sum).Sum / 1MB, 2)
        [PSCustomObject]@{
            DirName = $SDL_Item.FullName
            FileCount = $DirTreeInfo.Count
            Size_MB = $Size_MB
            }
        }
    }

# on screen
$Results

# send to CSV
$Results |
    Export-Csv -LiteralPath "$env:TEMP\TomMaleski_-_DirTreeSizeList.csv" -NoTypeInformation

截断输出到屏幕...

DirName                                     FileCount Size_MB
-------                                     --------- -------
C:\Temp\Logitech\Logitech_Gaming_Software_1       126    1.09
C:\Temp\BackupSets - Copy\2019-05-19                2       0
C:\Temp\BackupSets - Copy\2019-05-20                2       0
C:\Temp\chocolatey\ConEmu                           1    6.85

[*...snip...*] 

C:\Temp\zzz - Copy\test 4.poiu                      0       0
C:\Temp\zzz - Copy\TestingStuff                    40   66.16
C:\Temp\zzz - Copy\TestingStuff - Copy             40   66.16
C:\Temp\zzz - Copy\Users                           35   66.16
C:\Temp\zzz - Copy\Users - Copy                    35   66.16

截断的CSV文件内容...

"DirName","FileCount","Size_MB"
"C:\Temp\Logitech\Logitech_Gaming_Software_1","126","1.09"
"C:\Temp\BackupSets - Copy\2019-05-19","2","0"
"C:\Temp\BackupSets - Copy\2019-05-20","2","0"
"C:\Temp\chocolatey\ConEmu","1","6.85"

[*...snip...*] 

"C:\Temp\zzz - Copy\test 4.poiu","0","0"
"C:\Temp\zzz - Copy\TestingStuff","40","66.16"
"C:\Temp\zzz - Copy\TestingStuff - Copy","40","66.16"
"C:\Temp\zzz - Copy\Users","35","66.16"
"C:\Temp\zzz - Copy\Users - Copy","35","66.16"

答案 1 :(得分:0)

一个带有别名的班轮:

gci P:\*\* -Directory | %{"{0}; {1}" -f $_.FullName,(gci $_ -File -Recurse|measure Length -sum).Sum}

我的Q:\Test\*文件夹的输出示例

Q:\Test\2015; 14977618
Q:\Test\2016; 6847244
Q:\Test\2017; 40056457
Q:\Test\2018; 405594085
Q:\Test\2019; 1551213