【问题标题】:Powershell get number of files and folders in directoryPowershell获取目录中文件和文件夹的数量
【发布时间】:2014-08-13 23:42:48
【问题描述】:

我将编写一个 powershell 脚本来获取它所在目录的各种统计信息。

如果我这样做了

get-childitem

我明白了

d----        06/23/2014   2:49 PM            asdf;
-a---        06/23/2014   2:49 PM         23 New Text Document - Copy (2).txt
-a---        06/23/2014   2:49 PM         23 New Text Document - Copy (3).txt
-a---        06/23/2014   2:49 PM         23 New Text Document - Copy (4).txt
-a---        06/23/2014   2:49 PM         23 New Text Document - Copy (5).txt
-a---        06/23/2014   2:49 PM         23 New Text Document - Copy.txt
-a---        06/23/2014   2:49 PM         23 New Text Document.txt

如果我这样做了

(get-childitem).count

我明白了

7

我希望能够计算目录中的文件数以及文件夹数。任何建议,将不胜感激。谢谢。

【问题讨论】:

    标签: batch-file powershell scripting cmd


    【解决方案1】:

    嗯,你想要两个不同的东西,所以你可能需要运行两个不同的命令。计数文件夹:

    (GCI|?{$_.PSIsContainer}).Count
    

    然后统计文件数:

    (GCI|?{!$_.PSIsContainer}).Count
    

    【讨论】:

      【解决方案2】:

      您可以使用以下命令执行此操作:

      $headers = @{ $true='Folder'; $false='File' }
      Get-ChildItem |
        Group-Object PSIsContainer |
        Select-Object @{ Name="Type"; Expression={ $headers[$_.Name -eq $true] } }, Count
      

      或全部在一行中:

      gci | group psiscontainer | select @{n="Type";e={@{$true='Folder';$false='File'}[$_.Name -eq $true]}}, Count
      

      【讨论】:

      • 简化了一点...Get-ChildItem | group @{Expression={if($_.PSISContainer){'Folder'} else {'File'}}} -NoElement
      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2020-10-07
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多