Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script to count specific files in directories & subdirectories

I am trying to make a powershell script which will count all the .xml files in directories & subdirectories and list the value + path.

So far I have done the this:

Get-ChildItem -Path c:/test -recurse -include *.xml

Which is close to what I want but without the actual files names, just the folder paths and counts.

This is what I am getting:

>     Directory: C:\test\A_Sub-folder
> Mode                LastWriteTime     Length Name
> ----                -------------     ------ ----
> -a---        27/11/2015     11:29          0 AA.xml
> 
>     Directory: C:\test 
> Mode                LastWriteTime     Length Name
> ----                -------------     ------ ----
> -a---        27/11/2015     11:29          0 BB.xml
> -a---        27/11/2015     11:29          0 CC.xml
> -a---        27/11/2015     11:29          0 DD.xml

And I'm trying to get this ( or similar):

 >  Directory: C:\test\A_Sub-folder
 > 1
 >  Directory: C:\test 
 > 3

The plan is to run this script on each root drive (some drives have around 5k .xml files, so I am not sure how this will affect performance.)

Edit:

This works perfectly for subfolders but for some reason it doesn't work under root drive directories (eg. e:/). I'm trying to exclude \windows and \program files but it doesn't work. Is there any way of excluding root directories in the search?

Script so far:

$excludedPaths = @("E:\Windows", "E\Program Files", "E:\Program Files (x86)", "E:\MSSQL", "E:\MSSQL11.MSSQLSERVER");
$pathtocount = Read-Host -Prompt 'Input path to count xml files'
Get-ChildItem -Path $pathtocount -recurse -include *.xml | Where-Object { $excludedPaths -notcontains $_.Directory } | Group-Object -Property Directory | Sort-Object count
like image 549
daelas Avatar asked Dec 04 '15 16:12

daelas


1 Answers

Get-ChildItem -Path c:/test -recurse -filter *.xml | Group-Object -Property Directory

And to get a nicer table, you could add -NoElement to Group-Object.

To exclude some directories, try:

$excludedPaths = @("Windows", "Program Files", "Program Files (x86)");
$searchPaths = Get-ChildItem -Path C:\ -Directory | Where-Object { $excludedPaths -notcontains $_.name }
Get-ChildItem -Path $searchPaths -recurse -filter *.xml | Group-Object -Property Directory

Update: According to Get-ChildItem documentation, -filter is more efficient than -include when you only filter for one pattern. (If you filter for more patterns, -filter doesn't work and you have to use -include)

For excluding whole subtrees:

  • -exclude doesn't work, because it is applied to each file, it doesn't prune the whole tree, and it seems the filter is only matched to the filename, not the directory
  • your Where-Object doesn't work because $_.Directory returns nothing (not sure why, I haven't found any documentation)
  • Even if looking at another property ($_.FullName seems to do what you intend), this would only remove the directory itself, not paths starting with the directory. You would need to do a string-prefix-match (using -imatch or -ilike) against each string in the filter-set
like image 124
Rolf Rander Avatar answered Sep 27 '22 17:09

Rolf Rander