I'm trying to list the folderstructure of a disc and each folders size.
I've got the folderstructure down, now i just need to output each folders size.
According to https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir there is no flag for displaying filesize - only hiding it. I'm guessing i've gone the wrong route here, but any help is appreciated.
This is what i've got so far:
dir /s /b /o:n /a:d > "C:\folderlist.txt"
Expected output:
C:\WINDOWS\system32\downlevel 400mb
C:\WINDOWS\system32\drivers 100mb
C:\WINDOWS\system32\DriverState 4kb
C:\WINDOWS\system32\DriverStore 1kb
C:\WINDOWS\system32\DRVSTORE 1gb
The acronym for the filesize i.e. (mb, kb, gb, tb) doesn't matter. As long as it shows the foldersize in some quantifiable manner.
Powershell alternative is also welcome.
A PowerShell solution that builds on montonero's helpful answer and improves the following aspects:
Sample calls, based on function Get-DirectorySize
defined below:
# Get the size of the current directory (only).
Get-DirectorySize
# As requested by the OP:
# Recursively report the sizes of all subdirectories in the current directory.
Get-DirectorySize -Recurse -ExcludeSelf
# Get the size of all child directories and sort them by size, from largest
# to smallest, showing only the 5 largest ones:
Get-DirectorySize -Depth 1 -ExcludeSelf |
Sort-Object Size -Descending |
Select-Object -First 5
Sample output from the last command:
FullName FriendlySize Size
-------- ------------ ----
C:\Users\jdoe\AppData 3.27gb 3514782772
C:\Users\jdoe\Desktop 801.40mb 840326199
C:\Users\jdoe\.nuget 778.97mb 816814396
C:\Users\jdoe\.vscode 449.12mb 470931418
C:\Users\jdoe\Projects 104.07mb 109127742
Note that property .FriendlySize
contains a friendly, auto-scaled string representation of the size, whereas .Size
is a number ([long]
) containing the actual byte count, which is what facilitates further programmatic processing.
Note: Adding properties to the output objects that facilitate friendly display only is done here for implementation convenience only. The proper Powershell way would be to instead define formatting instructions based on the output object type - see the docs.
Caveats (apply to the linked answer too):
Only logical sizes are reported, i.e., the actual bytes need by the file data, which differs from the size on disk, which is, typically, larger, due to files occupying fixed-size blocks; conversely, compressed and sparse files occupy less disk space.
The implementation of the recursion (with -Recurse
and / or -Depth
) is inefficient, because the subtree of each directory encountered is scanned in full; this is helped somewhat by the filesystem cache.
Get-DirectorySize
source codeNote: Requires Windows PowerShell v3+; also compatible with PowerShell Core.
function Get-DirectorySize
{
param(
[Parameter(ValueFromPipeline)] [Alias('PSPath')]
[string] $LiteralPath = '.',
[switch] $Recurse,
[switch] $ExcludeSelf,
[int] $Depth = -1,
[int] $__ThisDepth = 0 # internal use only
)
process {
# Resolve to a full filesystem path, if necessary
$fullName = if ($__ThisDepth) { $LiteralPath } else { Convert-Path -ErrorAction Stop -LiteralPath $LiteralPath }
if ($ExcludeSelf) { # Exclude the input dir. itself; implies -Recurse
$Recurse = $True
$ExcludeSelf = $False
} else { # Process this dir.
# Calculate this dir's total logical size.
# Note: [System.IO.DirectoryInfo].EnumerateFiles() would be faster,
# but cannot handle inaccessible directories.
$size = [Linq.Enumerable]::Sum(
[long[]] (Get-ChildItem -Force -Recurse -File -LiteralPath $fullName).ForEach('Length')
)
# Create a friendly representation of the size.
$decimalPlaces = 2
$padWidth = 8
$scaledSize = switch ([double] $size) {
{$_ -ge 1tb } { $_ / 1tb; $suffix='tb'; break }
{$_ -ge 1gb } { $_ / 1gb; $suffix='gb'; break }
{$_ -ge 1mb } { $_ / 1mb; $suffix='mb'; break }
{$_ -ge 1kb } { $_ / 1kb; $suffix='kb'; break }
default { $_; $suffix='b'; $decimalPlaces = 0; break }
}
# Construct and output an object representing the dir. at hand.
[pscustomobject] @{
FullName = $fullName
FriendlySize = ("{0:N${decimalPlaces}}${suffix}" -f $scaledSize).PadLeft($padWidth, ' ')
Size = $size
}
}
# Recurse, if requested.
if ($Recurse -or $Depth -ge 1) {
if ($Depth -lt 0 -or (++$__ThisDepth) -le $Depth) {
# Note: This top-down recursion is inefficient, because any given directory's
# subtree is processed in full.
Get-ChildItem -Force -Directory -LiteralPath $fullName |
ForEach-Object { Get-DirectorySize -LiteralPath $_.FullName -Recurse -Depth $Depth -__ThisDepth $__ThisDepth }
}
}
}
}
Here's the comment-based help for the function; if you add the function to, say, your $PROFILE
, place the help directly above the function or just inside the function body in order to get support for -?
and automatic integration with Get-Help
.
<#
.SYNOPSIS
Gets the logical size of directories in bytes.
.DESCRIPTION
Given a literal directory path, output that directory's logical size, i.e.,
the sum of all files contained in the directory, including hidden ones.
NOTE:
* The logical size is distinct from the size on disk, given that files
are stored in fixed-size blocks. Furthermore, files can be compressed
or sparse.
Thus, the size of regular files on disk is typically greater than
their logical size; conversely, compressed and sparse files require less
disk space.
Finally, the list of child items maintained by the filesystem for each
directory requires disk space too.
* Wildcard expressions aren't directly supported, but you may pipe in
Output from Get-ChildItem / Get-Item; if files rather than directotries
happen to be among the input objects, their size is reported as-is.
CAVEATS:
* Can take a long time to run with large directory trees, especially with
-Recurse.
* Recursion is implemented inefficently.
.PARAMETER LiteralPath
The literal path of a directory. May be provided via the pipeline.
.PARAMETER Recurse
Calculates the logical size not only of the input directory itself, but of
all subdirectories in its subtree too.
To limit the recursion depth, use -Depth.
.PARAMETER Depth
Limits the recursion depth to the specified number of levels. Implies -Recurse.
Note that 0 means no recursion. Use just -Recurse in order not to limit the
recursion.
.PARAMETER ExcludeSelf
Excludes the target directory itself from the size calculation.
Implies -Recurse. Since -Depth implies -Recurse, you could use -ExcludeSelf
-Depth 1 to report only the sizes of the immediate subdirectories.
.OUTPUTS
[pscustomobject] instances with properties FullName, Size, and FriendlySize.
.EXAMPLE
Get-DirectorySize
Gets the logical size of the current directory.
.EXAMPLE
Get-DirectorySize -Recurse
Gets the logical size of the current directory and all its subdirectories.
.EXAMPLE
Get-DirectorySize /path/to -ExcludeSelf -Depth 1 | Sort-Object Size
Gets the logical size of all child directories in /path/to without including
/path/to itself, and sorts the result by size (largest last).
#>
You can't get folder sizes with dir
. You'll need to individually count size for every folder recursively. There's a number of working Powershell examples. This one is pretty nice https://md3v.com/getting-a-folder-tree-size-with-powershell :
function tree($startFolder)
{
$colItems = Get-ChildItem $startFolder | 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 | Select-Object Sum
$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
tree $i.FullName
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With