簡體   English   中英

Powershell文件夾大小排序與自動大小轉換

[英]Powershell folder size sort with auto size conversion

我有一個PowerShell腳本,該腳本獲取文件夾名稱,LastWriteTime,大小

cd \myfolder    
get-childitem | where {$_.PSIsContainer} | foreach { 
    $size = ( Get-ChildItem $_ -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum 
    $obj = new-object psobject
    add-member -inp $obj noteproperty path $_.Name
    add-member -inp $obj noteproperty time $_.LastWriteTime
    add-member -inp $obj noteproperty size $size 
    $obj  
    }

結果路徑/名稱,LastWriteTime,大小

path                                    time                                                                       size
----                                    ----                                                                       ----
Audit_data_network_8-FEB-2017           2/8/2017 10:59:33 AM                                                    1071084
dec-2015                                1/25/2016 10:05:07 AM                                                  29742775
games                                   2/15/2016 11:33:02 AM                                                  52134862
kolachi                                 12/2/2015 3:37:27 PM                                                   12487862
lighroom_                               5/29/2015 2:13:10 PM                                                    2788765
Mini_Remote_Control7.5.9.0_Portable     6/13/2014 3:58:08 PM                                                   52406834
ps                                      2/20/2017 4:23:10 PM                                                     126707
totalcmd                                1/25/2017 4:20:48 PM                                                   11113908

我想修改結果以添加以下功能

  • 自動以KB / Mb / GB顯示大小
  • 按大小對結果進行排序(上面的“最常用的文件夾大小”)

要添加自動轉換功能,我在某個論壇上發現了以下內容

# If ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
#ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
#ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
#ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
#ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
#ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" } 
#Write-Output  $sizeOutput  

但是由於我對腳本問題缺乏了解,如何將其與現有腳本合並並獲得我想要的結果,因此我過去2-3天的工作陷入了困境。非常感謝您的幫助...

您可以將邏輯放在ex中。 一個可重用的函數,並使用該函數生成size的屬性值。 在此解決方案中,我使用計算所得的屬性而不是函數。 這樣,我們可以保留原始大小(長度的總和)以進行排序,然后修改輸出。 我還簡化了您的對象創建。 例如:

Get-FoldersWithSize.ps1:

param ($Path = ".")

$PrettySizeColumn = @{name="Size";expression={
    $size = $_.Size
    if ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
    ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
    ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
    ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
    ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
    ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" } 
    $sizeOutput
}}

Get-ChildItem -Path $Path | Where-Object {$_.PSIsContainer} | ForEach-Object { 
    $size = ( Get-ChildItem -Path $_.FullName -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum 
    $obj = new-object -TypeName psobject -Property @{
        Path = $_.Name
        Time = $_.LastWriteTime
        Size = $size
    }
    $obj  
} | Sort-Object -Property Size -Descending | Select-Object Path, Time, $PrettySizeColumn

用法:

#Current directory (default value)
.\Get-FoldersWithSize.ps1

#Relative path
.\Get-FoldersWithSize.ps1 -Path ".\Downloads"

#Absolute path
.\Get-FoldersWithSize.ps1 -Path "C:\ProgramData"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM