递归查找与扩展名匹配的文件,然后根据父目录返回这些文件的计数

时间:2020-12-24 00:24:48

标签: powershell file logging find

我对 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

1 个答案:

答案 0 :(得分:3)

大功告成,您只需要使用 Group-Object 函数,而不是弄乱路径并尝试手动计数。

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

从那里可以很容易地根据需要进行格式化。

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