Reputation: 5697
I have folders with different count of files. How to check if there are some files and how many? I use this, but when there is only one file, it gives me nothing. Thank you.
$number_of_files = Get-Childitem c:\folder -name -force
$number_of_files.Count
Upvotes: 4
Views: 386
Reputation: 2884
Try
(Get-ChildItem c:\folder -name -force | where {$_.GetType() -match "fileInfo"} | measure-object).count
Upvotes: 2
Reputation: 1168
The answer from @Marek works, but it perhaps not the most efficient. Rather than doing a .GetType() and doing a regex match, why not just filter based on a property in folder and not in file object. IE:
(Get-ChildItem c:\folder -name -force | where {! $_.PSIsContainer} | measure-object).count.
Upvotes: 3
Reputation: 60976
Change it like this:
$number_of_files = @(Get-Childitem c:\folder -name -force)
$number_of_files.Count
This force $number_of_files
to be always an Array and have the count property rigth set
also for value of 1
Upvotes: 5
Reputation: 4878
I use measure
for this:
$f = Get-Childitem c:\folder -name -force
($f | measure).Count
Upvotes: 1