未列出子目录的文件夹的 Powershell 文件夹大小

编程入门 行业动态 更新时间:2024-10-28 00:29:24
本文介绍了未列出子目录的文件夹的 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 文件夹大小

本文发布于:2023-10-08 23:55:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   子目录   大小   Powershell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!