简体   繁体   English

递归计算子文件夹和组结果中的过滤文件

[英]Recursively count filtered files in subfolders and group results

I want to count all files with a particular prefix in a directory and then display the results based on each sub directory. 我想计算目录中具有特定前缀的所有文件,然后根据每个子目录显示结果。

The directory tree is as follows 目录树如下

Test
  January
     sms20180101_110.unl
     rec20180101_110.unl
     data20180101_110.unl
  February
     sms20180201_110.unl
     rec20180201_110.unl
     data20180201_110.unl
  March
    sms20180301_110.unl
    rec20180301_110.unl
    data20180301_110.unl

So, I need to count for example the total data files in each subdirectory and display results as follows 因此,我需要计算例如每个子目录中的总数据文件并显示结果,如下所示

January      1
February     5
March        10   

I wrote the following command in Powershell 我在Powershell中编写了以下命令

Get-ChildItem -Path D:\Test -Filter *data* -Force -Recurse | Measure-Object | %{$_.Count}

So, the problem is it is giving me the total files in the root directory 所以,问题是它给了我根目录下的全部文件

A similar question was asked here Recursively count files in subfolders but I have not been able to customize the solutions provided here to my need 在这里提出了类似的问题,以递归方式计算子文件夹中的文件,但是我无法根据我的需要自定义此处提供的解决方案

Based on your scenario, you can use Group-Object like this - 根据您的情况,您可以像这样使用Group-Object

Get-ChildItem -Path D:\Test -Filter *data* -Force -Recurse | Group-Object -Property Directory | Select-Object Name, Count

This will list all the name of the folders and sub-folders along with the count of files having data in it's name. 这将列出文件夹和子文件夹的所有名称,以及名称中包含数据的文件数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM