在Powershell中显示目录结构及其大小

发布于 2024-09-26 05:57:24 字数 928 浏览 0 评论 0原文

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

  1. 确定文件夹的大小 http://technet.microsoft.com/en-us/library/ff730945.aspx
  2. 用于获取目录总大小的 PowerShell 脚本获取目录总大小的 PowerShell 脚本

这些灵魂很棒,但我正在寻找类似于“dir”输出的东西,方便且简单我可以在文件夹结构中的任何位置使用它。

所以,我最终这样做了,有什么建议可以使它简单、优雅、高效。

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} 
                  }
     };  

谢谢。

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. 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

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} 
                  }
     };  

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2024-10-03 05:57:24

第一个小修改是避免为每个目录创建一个新的 FileSystemObject。使其成为一个函数并将新对象从管道中拉出。

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 来计算目录大小,如下所示:

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
}

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}  
                         } 
                     }
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文