未列出子目录的文件夹的 Powershell 文件夹大小 [英] Powershell folder size of folders without listing Subdirectories

查看:33
本文介绍了未列出子目录的文件夹的 Powershell 文件夹大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了几个使用以下脚本获取文件夹大小的资源

I have found several resources that use the following script to get folder sizes

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }

问题在于它还列出了子目录,即:

The problem with that is it also lists the subdirectories ie:

c:\test\1 -- 10mb
c:\test\1\folder -- 10mb
c:\test\1\folder\deep -- 5mb
c:\test\1\folder\tuna -- 5mb
c:\test\2 -- 20bm
c:\test\2\folder -- 20mb
c:\test\2\folder\deep -- 10mb
c:\test\2\folder\tuna -- 10mb

我想你知道我要去哪里.我正在寻找的只是父文件夹的结果......所以:

I think you know see where I am going. What I am looking for is just the parent folder's results... SO:

c:\test\1 -- 10mb
c:\test\2 -- 20mb

如何使用 Powershell 完成此操作?....

How can this be accomplished with Powershell? ....

推荐答案

需要递归获取每个目录的总内容大小来输出.此外,您需要指定要抓取的内容不是目录,否则可能会出错(因为目录没有 Length 参数).

You need to get the total contents size of each directory recursively to output. Also, you need to specify that the contents you're grabbing to measure are not directories, or you risk errors (as directories do not have a Length parameter).

这是为您要查找的输出修改的脚本:

Here's your script modified for the output you're looking for:

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

这篇关于未列出子目录的文件夹的 Powershell 文件夹大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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