【问题标题】:Recursively finding files that match an extension, and then returning the a count of those files based on parent directory递归查找与扩展名匹配的文件,然后根据父目录返回这些文件的计数
【发布时间】:2020-12-24 00:24:48
【问题描述】:

我对powershell很陌生,几乎所有东西都是从其他地方复制过来的,所以请多多包涵。

这是我目前所拥有的(它无法正常工作):

Get-ChildItem -Path "D:\" -Recurse -Filter *.log |
    Split-Path -Parent | 
        %{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count }

基本上我要做的是找出 *.log 文件可能在驱动器中的位置。由于我一次将其应用于许多主机,因此我想要这些文件的摘要。

我得到了什么:

D:\Game Files\appdata\BlackCipher\BlackCall.log
D:\Game Files\appdata\BlackCipher\BlackCipher.log
D:\Game Files\appdata\BlackCipher\BlackXchg.log
D:\Game Files\appdata\BlackCipher\NGClient.log
D:\Game Files\Counter-Strike Source\debug.log
D:\Game Files\Games\Furi\2016-07-08_164326\error.log
D:\Game Files\Hearthstone\bnet_client.log
D:\Game Files\Hearthstone\2014-01-06_155616\error.log
D:\Game Files\Hearthstone\2014-02-02_201924\error.log
D:\Game Files\Hearthstone\2014-02-02_201946\error.log
D:\Game Files\Hearthstone\2014-02-02_222450\error.log
D:\Game Files\Hearthstone\2015-06-24_082024\error.log
D:\Game Files\Hearthstone\2015-07-06_174250\error.log
D:\Game Files\Hearthstone\2015-07-25_180738\error.log
D:\Game Files\Hearthstone\2015-10-19_160134\error.log
D:\Game Files\Hearthstone\2016-07-13_164124\error.log
D:\Game Files\Hearthstone\2016-12-19_021056\error.log
D:\Game Files\Hearthstone\2017-08-27_132304\error.log


我想要什么:

D:\Game Files\appdata\BlackCipher 4
D:\Game Files\Counter-Strike Source 1
D:\Game Files\Games\Furi\2016-07-08_164326 1
D:\Game Files\Hearthstone\ 12

【问题讨论】:

    标签: powershell file logging find


    【解决方案1】:

    你快到了,你只需要使用Group-Object函数而不是弄乱路径并尝试手动计算它。

    Get-ChildItem -Path "D:\" -Recurse -Filter *.log | Group-Object -Property Directory -NoElement
    

    从那里可以很容易地按照您的喜好进行格式化。

    ... | ForEach-Object { '{0} {1}' -f $_.Name, $_.Count }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 2011-06-27
      • 2023-01-19
      • 2011-08-21
      相关资源
      最近更新 更多