Use selective criteria to count files in PowerShell
Use Windows PowerShell to easily return some key statistics about the files contained on your computer or network drives using selective criteria.
I recently needed to collect some statistics about the directories that I was working with. Whilst I wanted to conduct a recursive search, I only wanted to count files of a certain type and exclude those which sat in a certain specified subdirectory.
To get the job done, I turned to PowerShell.
Counting the size of files
To recursively count the size of a particular file type in a directory whilst excluding certain others, the following syntax can be used:
$totalsize=[long]0;gci $path -Recurse -include @("*.jpg") | Where-Object {$_.FullName -notlike "*Output*"} | %{$totalsize+=$_.Length};$totalsize/1000000000
In this example, the total size of all JPG files in the target directory is returned in gigabytes, excluding files in a subdirectory called /Output. You can also use this syntax to exclude JPG files with a certain string in their name because $_.FullName
checks the full path, not just the file name, so it can (and will) target either.
This code can be modified to return results in megabytes ($totalsize/1000000
) or kilobytes ($totalsize/1000
) if that is preferred.
Counting the number of files
To recursively count the number of a particular file type in a directory whilst excluding certain others, the following syntax can be used:
(gci $path -Recurse @("*.png", "*.jpg") | Where-Object {$_.FullName -notlike "*Output*"} | Measure-Object).Count
This example returns the total number of all PNG and JPG files in the target directory in gigabytes, excluding files in a subdirectory called /Output.
Count files based on creation (or modification) date
To recursively count the number of a particular file type in a directory created in the past year whilst excluding certain others, use the following syntax:
(gci $path -Recurse -include @("*.jpg") | Where-Object {($_.CreationTime -ge (Get-Date).AddDays(-365)) -and ({$_.FullName -notlike "*Output*"})} | Measure-Object).Count
This example returns the total number of JPG files in the target directory created in the last 365 days, excluding files in a subdirectory called /Output.
It’s easy to modify this code to include a timeframe. For instance, the following code will count the number of files created last week (ie between 7 and 14 days ago):
(gci $path -Recurse -include @("*.jpg") | Where-Object {($_.CreationTime -ge (Get-Date).AddDays(-14)) -and ($_.CreationTime -le (Get-Date).AddDays(-7)) -and ({$_.FullName -notlike "*Output*"})} | Measure-Object).Count
You can also specify a particular date to search:
(gci $path -Recurse -include @("*.jpg") | Where-Object {($_.CreationTime.Date.ToString("yyyy-MM-dd") -eq "2021-06-23") -and ({$_.FullName -notlike "*Output*"})} | Measure-Object).Count
Date ranges can also be specified:
(gci $path -Recurse -include @("*.jpg") | Where-Object {($_.CreationTime.Date.ToString("yyyy-MM-dd") -ge "2021-06-21") -and ($_.CreationTime.Date.ToString("yyyy-MM-dd") -le "2021-06-23") -and ({$_.FullName -notlike "*Output*"})} | Measure-Object).Count
This example returns the total number of JPG files in the target directory created between 21-23 June 2021 but excluding those in a subdirectory called /Output.
If you need to refer to the modification date rather than creation date, use $_.LastWriteTime
instead of $_.CreationTime
.
Comments
No comments have yet been submitted. Be the first!