PowerShell - 计算并输出文件夹大小 2 个文件夹深度

PowerShell - 计算并输出文件夹大小 2 个文件夹深度

我想运行一个 PowerShell 脚本来输出两层深度的文件夹大小。

我可以用这个手动完成:

function Get-Size
{
 param([string]$pth)
 "{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1mb) + " mb"
}

Write-Output "F:\DATA\MEDIA" > .\filesize.txt
Get-size F:\DATA\MEDIA >> .\filesize.txt

Write-Output "F:\DATA\MEDIA\MOVIES" >> .\filesize.txt
Get-Size F:\DATA\MEDIA\MOVIES >> .\filesize.txt

Write-Output "F:\DATA\MEDIA\MUSIC" >> .\filesize.txt
Get-Size F:\DATA\MEDIA\MUSIC >> .\filesize.txt

Write-Output "F:\DATA\MEDIA\TVSHOWS" >> .\filesize.txt
Get-Size F:\DATA\MEDIA\TVSHOWS >> .\filesize.txt

等等等等

但之后我必须手动输入每个文件夹。文件夹非常静态,但我似乎应该能够以某种方式自动执行此操作。

我显然是 PowerShell 的新手,非常感谢任何帮助。

最终,我想指定一个文件夹,并告诉它给出我指定位置两层深的文件夹中文件的大小。因此,从上面的例子来看,如果我声明:

Get-Size F:\DATA\MEDIA >> .\filesize.txt

最终我想要的是类似这样的输出:

 1,234,567 MB    F:\DATA\MEDIA
   734,567 MB     F:\DATA\MEDIA\MOVIES
   100,000 MB     F:\DATA\MEDIA\MUSIC
   400,000 MB     F:\DATA\MEDIA\TVSHOWS

文件夹的大小包括所有文件和子文件夹,但在输出中仅显示深层的文件夹级别。

谢谢您的帮助。

答案1

仔细查看 Get-ChildItem cmdlet 帮助文件。它有一个深度参数。

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-ChildItem).Parameters
(Get-Command -Name Get-ChildItem).Parameters.Keys
<#
# Results

Path
LiteralPath
Filter
Include
Exclude
Recurse
Depth
...
#>
Get-help -Name Get-ChildItem -Examples
Get-help -Name Get-ChildItem -Full
<#
# Results

...
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

Get-ChildItem doesn't display empty directories. When a Get-ChildItem command includes the Depth or Recurse parameters, empty directories aren't included in the output.
...
#>
Get-help -Name Get-ChildItem -Online

答案2

好吧,我无法深入两个文件夹,只能深入一个,但这仍然能完成它的工作并给我一个干净的输出。我只是想发布历史信息。在网上找到了一些代码,我拼凑起来并根据我的目的进行了修改。感谢编写代码的原始用户:

param(
    [string]
    $pth
    )

function Get-Size
{
    # Set variables to null
    $rootsize = $null
    $sumfoldernum = $null
    $foldertotal = $null

    # Check if path is even valid
    if ( -not ( Test-Path "$pth" ) ) { $pth + " is not a valid path"; ""; ""; return }

    # Output Path for log file
    $pth

    # Check Root Folder Size
    $rootsize = (Get-ChildItem -path $pth -recurse -force | measure-object -property length -sum).sum

    # Store as byte number as well as output file size in MB with fixed spacing
    $rootsizenum = $rootsize
    $rootsize = "{0:n2}" -f ( $rootsize / 1MB )
    ( $rootsize.ToString() ).padLeft(17) + " MB"
    "--------------------"

    # Check SubFolder Sizes
    $colItems = Get-ChildItem -path $pth | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
    foreach ($i in $colItems)
{
    $subFolderItems = ( Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum ).sum

    # Sum all sub folder sizes and output foldersize under root folder heading with individual folder size
    $sumfoldernum = $sumfoldernum + $subFolderItems
    $sumfolder = "{0:N2}" -f ( $subFolderItems / 1MB )
    $sumfolder = ( $sumfolder.ToString() ).padleft(17)
    $sumfolder + " MB" + " -- " + $i.FullName
}

    # Difference root folder from sub folder indicates total size of files in root
        $foldertotal = $rootsizenum - $sumfoldernum
        $foldertotal = "{0:N2}" -f ( $foldertotal / 1MB )
        ( $foldertotal.ToString() ).padleft(17) + " MB" + " -- Files in Root " + $pth
        ""
        ""
}

    # Start main code here
    $datetime = Get-Date -Format "yyyyMMdd_HHmmss"
    $logdir = ".\_logs\foldersize"
    $logfile = "foldersize_$datetime.txt"

    #check if log folder exists, if not then create it and post a file letting user (me) know it has been created
    if ( -not ( Test-Path $logdir ) ) { Write-Output "$(Get-Date) `n`n`Log folder does not exist`n`n`Creating log folder $logdir" | Tee-Object -File ".\FOLDERSIZE_LOGREADME_$datetime.txt" ; md $logdir | Tee-Object -File ".\FOLDERSIZE_LOGREADME_$datetime.txt" -Append}

    # Log Info
    $log = "$logdir\$logfile"

    Write-Output "Start Time: $(Get-Date)" | Tee-Object -File $log
    Write-Output "" | Tee-Object -File $log -Append

    # Execute path input (currently from windows batch)
    Get-Size "$pth" | Tee-Object -File $log -Append

    Write-Output "End Time: $(Get-Date)" | Tee-Object -File $log -Append

    Write-Host "`n`n`*** FOLDERSIZE LOGFILE FOR $pth SAVED TO $log"

输出至少看起来干净且简单:

Start Time: 05/11/2020 22:50:03

F:\DATA\ARCHIVE
           974.46 MB
--------------------
           969.35 MB -- F:\DATA\ARCHIVE\Documents
             5.11 MB -- F:\DATA\ARCHIVE\Games
             0.00 MB -- Files in Root F:\DATA\ARCHIVE


F:\DATA\GAMES
    11,387,344.94 MB
--------------------
       227,673.38 MB -- F:\DATA\GAMES\Battle.net
       333,559.87 MB -- F:\DATA\GAMES\Epic
     1,173,797.71 MB -- F:\DATA\GAMES\GOG
        35,448.62 MB -- F:\DATA\GAMES\Misc
       651,093.42 MB -- F:\DATA\GAMES\Origin
     8,463,138.48 MB -- F:\DATA\GAMES\STEAM
       502,633.46 MB -- F:\DATA\GAMES\UPlay
             0.00 MB -- Files in Root F:\DATA\GAMES


F:\DATA\MEDIA
     1,478,237.33 MB
--------------------
        55,904.54 MB -- F:\DATA\MEDIA\GAMEFILES
        15,865.23 MB -- F:\DATA\MEDIA\MUSIC
        80,535.78 MB -- F:\DATA\MEDIA\SOFTWARE
     1,325,931.78 MB -- F:\DATA\MEDIA\VIDEOS
             0.00 MB -- Files in Root F:\DATA\MEDIA


F:\DATA\OneDrive
       102,142.07 MB
--------------------
            24.01 MB -- F:\DATA\OneDrive\Archive
           917.32 MB -- F:\DATA\OneDrive\Documents
           371.58 MB -- F:\DATA\OneDrive\Files
           992.71 MB -- F:\DATA\OneDrive\Music
        99,836.44 MB -- F:\DATA\OneDrive\Pictures
             0.00 MB -- F:\DATA\OneDrive\Public
             0.00 MB -- Files in Root F:\DATA\OneDrive


F:\DATA\PCBACKUP
     2,648,954.21 MB
--------------------
       699,422.92 MB -- F:\DATA\PCBACKUP\Deathstar
       827,018.05 MB -- F:\DATA\PCBACKUP\Dell1
       747,457.08 MB -- F:\DATA\PCBACKUP\Dell2
       138,852.63 MB -- F:\DATA\PCBACKUP\Enterprise
       236,203.53 MB -- F:\DATA\PCBACKUP\P650HSG
             0.00 MB -- Files in Root F:\DATA\PCBACKUP


F:\DATA\USERS
       872,691.56 MB
--------------------
             0.00 MB -- F:\DATA\USERS\(redacted)
             0.00 MB -- F:\DATA\USERS\(redacted)
       859,441.17 MB -- F:\DATA\USERS\(redacted)
        13,250.39 MB -- F:\DATA\USERS\Public
             0.00 MB -- Files in Root F:\DATA\USERS


End Time: 05/11/2020 22:59:09

相关内容