Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell - how to check existence of one file

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
like image 907
culter Avatar asked Dec 20 '22 18:12

culter


2 Answers

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

like image 113
CB. Avatar answered Jan 14 '23 01:01

CB.


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.

like image 39
Thomas Lee Avatar answered Jan 14 '23 02:01

Thomas Lee