在Powershell中显示大小的目录结构 [英] Display directory structure with size in Powershell

查看:979
本文介绍了在Powershell中显示大小的目录结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用显示子文件夹和文件大小的dir命令。
在搜索powershell目录大小之后,我发现两个有用的链接

Trying to have "dir" command that displays size of the sub folders and files. After googling "powershell directory size", I found thew two useful links


  1. 确定文件夹的大小 http://technet.microsoft.com/en-us/library/ff730945.aspx

  2. 获取目录的PowerShell脚本总大小 PowerShell脚本以获取目录的总大小

  1. Determining the Size of a Folder http://technet.microsoft.com/en-us/library/ff730945.aspx
  2. PowerShell Script to Get a Directory Total Size PowerShell Script to Get a Directory Total Size

这些soultions是伟大的,但我正在寻找一些东西类似dir输出,方便简单,我可以使用文件夹结构中的任何地方。

These soultions are great, but I am looking for something resembles "dir" output, handy and simple and that I can use anywhere in the folder structure.

所以,我最终这样做,任何建议,使它简单,优雅,高效。

So, I ended up doing this, Any suggestions to make it simple, elegant, efficient.

Get-ChildItem | 
Format-Table  -AutoSize Mode, LastWriteTime, Name,
     @{ Label="Length"; alignment="Left";
       Expression={ 
                    if($_.PSIsContainer -eq $True) 
                        {(New-Object -com  Scripting.FileSystemObject).GetFolder( $_.FullName).Size}  
                    else 
                        {$_.Length} 
                  }
     };  

谢谢。

推荐答案

第一个minor mod将是避免为每个目录创建一个新的FileSystemObject。使这个功能并将新对象拉出管道。

The first minor mod would be to avoid creating a new FileSystemObject for every directory. Make this a function and pull the new-object out of the pipeline.

function DirWithSize($path=$pwd)
{
    $fso = New-Object -com  Scripting.FileSystemObject
    Get-ChildItem | Format-Table  -AutoSize Mode, LastWriteTime, Name, 
                    @{ Label="Length"; alignment="Left"; Expression={  
                         if($_.PSIsContainer)  
                             {$fso.GetFolder( $_.FullName).Size}   
                         else  
                             {$_.Length}  
                         } 
                     }
}

如果你想完全避免COM,使用PowerShell计算目录大小,如下所示:

If you want to avoid COM altogether you could compute the dir sizes using just PowerShell like this:

function DirWithSize($path=$pwd)
{
    Get-ChildItem $path | 
        Foreach {if (!$_.PSIsContainer) {$_} `
                 else {
                     $size=0; `
                     Get-ChildItem $_ -r | Foreach {$size += $_.Length}; `
                     Add-Member NoteProperty Length $size -Inp $_ -PassThru `
                 }} |
        Format-Table Mode, LastWriteTime, Name, Length -Auto
}

这篇关于在Powershell中显示大小的目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆