目录大小错误

发布于 2025-01-05 18:41:32 字数 595 浏览 0 评论 0原文

我有一个脚本来检查文件夹及其所有子文件夹的大小,它可以满足我的需要,但如果文件夹大小为 0,它会抛出错误。我想添加一些逻辑,但我似乎找不到好方法,提前感谢您的帮助。

脚本是:

$startFolder = "C:\"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

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

I have a script to check the size of a folder and all of its sub-folders and it does what I need but it throws errors if the folder has size 0. I would like to add some logic in but I cant seem to find a good way to do it, thanks in advance for the help.

The script is:

$startFolder = "C:\"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

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

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

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

发布评论

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

评论(3

余厌 2025-01-12 18:41:32

最后我得到了这个。当一个文件夹没有文件时,即使它有非空文件夹,也会发生此错误。 EBGreen 发布的解决方案并不完整,因为它只考虑了子文件。

正确的脚本是:

$folder = $args[0]
[console]::WriteLine($folder)
$startFolder = $folder

#here we need the size of all subfiles and subfolders. Notice the -Recurse
$colItems = (Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
"------"

#here we take only the first level subfolders. Notice the Where-Object clause and NO -Recurse
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $i.FullName
        #here we need again the size of all subfiles and subfolders, notice the -Recurse
        $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object -property length -sum)
        "                                   -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }

现在没有错误并且值是准确的。

Finally I got this. The error happens when one folder has no file, even if it has non-empty folders. The solution posted by EBGreen is incomplete, because it takes only sub-files into consideration.

The correct script is:

$folder = $args[0]
[console]::WriteLine($folder)
$startFolder = $folder

#here we need the size of all subfiles and subfolders. Notice the -Recurse
$colItems = (Get-ChildItem $startFolder -Recurse | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"
"------"

#here we take only the first level subfolders. Notice the Where-Object clause and NO -Recurse
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $i.FullName
        #here we need again the size of all subfiles and subfolders, notice the -Recurse
        $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object -property length -sum)
        "                                   -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }

Now there's no error and the values are accurate.

如何视而不见 2025-01-12 18:41:32

这对我有用,没有错误:

$startFolder = "C:\"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

$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 -ErrorAction SilentlyContinue)
    $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}

this worked for me without errors:

$startFolder = "C:\"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

$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 -ErrorAction SilentlyContinue)
    $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
}
风吹雨成花 2025-01-12 18:41:32

问题在于,对于空文件夹,Get-ChildItem 的输出为 $null。 Measure-Object 需要一个具有长度属性的输入对象。因此,您可以省略对空文件夹的处理,如下所示:

foreach ($i in $colItems)
{
    if ($i.GetFileSystemInfos().Count) {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }
}

The issue is that the output of Get-ChildItem is $null for empty folders. Measure-Object is expecting an input object with a property of length. So you can just omit that processing for folders are empty like this:

foreach ($i in $colItems)
{
    if ($i.GetFileSystemInfos().Count) {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文