Hello, I am trying to count the number of files in a given folder. I pass the folder path as a parameter. i.e. F:\SQLServerBackupFile\Test.
There are two separate groups of files that I need to retrieve the count for then store them in two separate variables.
The first set of files are namedFile_1.bak.gpg, File_2.bak.gpg, etc. Currently, there are 12 files and periodically a new file will be added.
I have successfully captured a count using the following to count all files, except those with “Dispatch” in the name. I use where {! $_.PSIsContainer} because there is a subfolder and if the where clause is not used it adds one to the count.
$fileDirectory=’F:\SQLServerBackupFile\Test’
$fileCount = $directoryCount = $fileDispatchCount = 0
$fileCount= Get-ChildItem -path $fileDirectory -Exclude *Dispatch*.* | where {! $_.PSIsContainer}
Write-Host 'File Count entering loop: '$fileCount.CountFor the second variable I tried to count only the “Dispatch” files. I have tried a couple variations of the command and none work.
#Does not work $fileDispatchCount= Get-ChildItem -path $fileDirectory -Include *Dispatch*.* | Where-Object {$_.name -like "*Dispatch*"}
$fileDispatchCount= Get-ChildItem -path $fileDirectory -Include *Dispatch*.* | Where-Object {! $_.PSIsContainer}
Write-Host 'Dispatch File Count entering loop: '$fileDispatchCount.Count
Could someone please help with the second command?
Thank you!