Is there a Window's Command Line Option to Show the Sizes of Directories

0

When I do a dir command it, gives this information:

02/11/2015  01:39 PM    <DIR>          bar
11/11/2014  07:22 AM    <DIR>          buz
02/12/2015  01:35 PM       140,660,736 foo.sdf
01/21/2015  02:04 PM            10,505 foo.sln
01/21/2015  02:04 PM               256 foo.vssscc

Is there a switch or even a PowerShell command that I can do which will list all directories with their respective sizes?

Jonathan Mee

Posted 2015-03-06T18:54:43.920

Reputation: 169

2

See [so] question How to list all folder with size via batch file

– DavidPostill – 2015-03-06T19:09:59.080

not really a direct answer but cygwin http://pastebin.com/raw.php?i=VAD7x7Lr list directories find -type d list directories with sizes du -h

– barlop – 2015-03-06T23:34:21.553

@barlop sadly no access to Cygwin. It has to be PowerShell it seems. – Jonathan Mee – 2015-03-08T02:27:16.363

Answers

2

Trivial in PowerShell.

$FolderSize = Get-ChildItem $FolderPath -Recurse -Force | Measure-Object -Property Length -Sum;

$FolderSize.Sum;       #Size in bytes
$FolderSize.Sum / 1MB; #Size in MB
$FolderSize.Sum / 1GB; #Size in GB

To get each folder in a specified folder and calculate each, just iterate through them:

$BaseFolder = Get-ChildItem 'C:\Path\To\Folder';

$Results = @();

foreach ($f in $BaseFolder) {
    if ($f.PSIsContainer -eq $true) {
        $Size = Get-ChildItem $f -Recurse -Force | Measure-Object -Property Length -Sum;
    }
    else {
        $Size = Get-ChildItem $f | Measure-Object -Property Length -Sum;
    }
    $Results += New-Object PSObject -Property @{Name = $f.Name; Length = $Size.Sum;}
}

$Results | Format-Table -AutoSize;

Bacon Bits

Posted 2015-03-06T18:54:43.920

Reputation: 6 125

This gives me the total size of the current directory. It will not "list all directories with their respective sizes" as the question asks. – Jonathan Mee – 2015-03-09T11:47:50.813

@JonathanMee See update. – Bacon Bits – 2015-03-09T17:39:03.160

That is perfect, and wonderful. Can you add a comment in the answer that $BaseFolder = Get-ChildItem 'C:\Path\To\Folder'; needs to be set with the path to the folder you want to do the "dir" in? I just tried the code as-is and that obviously didn't work. I just want the next person through to be able to plug and play your code. – Jonathan Mee – 2015-03-09T18:37:27.750