powershell - 列出名称和大小的目录

标签 powershell directory size get-childitem

我想列出具有全名,大小,LastWriteTime的目录,然后递归到第三级。

Get-ChildItem -Depth 3 |
    Where-Object {
        $_.PSIsContainer
        } |
    ForEach-Object {
        $_.FullName + ";" + "{0:N2}" -f ((Get-ChildItem $_ -Recurse |
            Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) + " MB"+";"+ $_.LastWriteTime
        }

但是大小仅针对第一级目录进行度量。

输出为:
C:\folder1\folder2 ; 487,69 MB ; 30.01.2020 17:58:45
C:\folder1\folder2\folder3 ; 0,00 MB ; 30.01.2020 17:59:34
C:\folder1\folder2\folder3\folder4 ; 0,00 MB ; 30.01.2020 17:59:37

但是应该这样,因为我在每个文件夹中都放置了相同的文件:
C:\folder1\folder2 ; 1463,07 MB ; 30.01.2020 17:58:45
C:\folder1\folder2\folder3 ; 975,38 MB ; 30.01.2020 17:59:34
C:\folder1\folder2\folder3\folder4 ; 487,69 MB ; 30.01.2020 17:59:37

最佳答案

这将输出目录的FullNameSizeLastWriteTime。请注意,如果size为空,则什么也不输出:

Get-ChildItem -Depth 3 | 
    Where-Object { $_.PSIsContainer} |
    foreach{
        $size = "{0:N2}" -f ((Get-ChildItem $_ -Recurse | 
            Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB)

        if ($size){
            Write-Host $_.FullName  ";"  $size "MB" ";" $_.LastWriteTime
        }
    }
让我们知道您的 body 情况如何。
更新资料
更新您的问题后,请尝试以下代码:
Get-ChildItem -Depth 3 | 
    Where-Object { $_.PSIsContainer} |
    foreach{
        $folders = Get-ChildItem $_.FullName -Recurse

        [int]$totalSize = 0
        
        foreach ($folder in $folders){
            $totalSize += (Get-ItemProperty -Path $folder.FullName -Name Length -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Length)
        }

        if ($totalSize){
            Write-Host $_.FullName  ";"  $totalSize ";" $_.LastWriteTime
        }
    }
它可能可以清理,我只想看看它是否如您所愿:)

关于powershell - 列出名称和大小的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59990052/

相关文章:

c++ - 如何在可变大小的类中创建指针数组?

c++ - 静态数组的大小

scripting - 相当于powershell中的bash "expect"

java - 使用 servlet 在 WEB 文件夹中创建新目录不起作用

powershell - Powershell计划任务报告-将状态从数字更改为单词

windows - Windows 中目录/文件夹的 TreeView ?

vba - 是否可以列出自定义目录中的所有文件和文件夹 - excel vba

android - 可以更改 ActionBar 的文本大小/颜色吗?

powershell - 如何在 PowerShell 中连接值?

powershell - 另一个具有相同属性identifierUris值的对象已存在