locked
GetChildItem count returns null if there is one file in a folder RRS feed

  • Question

  • Hey folks.  I'm an absolute newbie with PS so please forgive me if this question is ridiculously basic.  I'm using the following code to see if a folder has files in it:

    $imagefiles = Get-ChildItem $filespec
    if ($imagefiles.count -gt 0) { Write-Host Files found! }
    else { Write-Host Folder is empty! }

    Everything is working almost perfectly.  If the folder has no files in it, it executes the Else condition.  If it has 2 or more files in it, it executes the code in the first set of braces.  Where it goes wonky is if the folder only has a single file in it.  In that situation $imagefile.count is actually coming back as $null instead of 1.  I'm using PS 2.0.

    Am I doing something really dumb?  TIA if you can offer any assistance!

    Tuesday, June 14, 2016 11:11 PM

Answers

  • Hi,

    In powershell 2 you should use @() operator when getting count property: 

    $imagefiles = Get-ChildItem $filespec 
     if (@($imagefiles).count -gt 0) { Write-Host Files found! }
     else { Write-Host Folder is empty! }
    Good luck)
    • Proposed as answer by Hello_2018 Thursday, June 16, 2016 2:31 AM
    • Marked as answer by Hello_2018 Friday, July 1, 2016 3:00 AM
    Wednesday, June 15, 2016 3:55 AM

All replies

  • Try this for simplicity it will give you a big clue as to how PowerShell works:

    if($imagefiles = Get-ChildItem $filespec){ 
    	Write-Host 'Files found!' -fore green
    }else{
    	Write-Host 'Folder is empty!' -fore red
    }


    \_(ツ)_/

    Tuesday, June 14, 2016 11:27 PM
  • We can also do this:

    if($imagefiles = Get-ChildItem $filepath -include *.jpg,*.png,*.bmp){ 
    	Write-Host 'Files found!' -fore green
    }else{
    	Write-Host 'Folder is empty!' -fore red
    }


    \_(ツ)_/

    Tuesday, June 14, 2016 11:29 PM
  • For a clue as to why your original code was failing see this:

    [array]$imagefiles = Get-ChildItem $filespec 
    if($imagefiles.count -gt 0){
    	Write-Host "$($imagefiles.Count) Files found!"
    }else{
    	Write-Host Folder is empty!
    }


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Thursday, June 16, 2016 2:30 AM
    Tuesday, June 14, 2016 11:34 PM
  • Thanks jrv, I used your code and it's now correctly handling a folder with a single file.  I assume this is because when there are two files Get-ChildItem is returning an array but if only one file exists it's returning a single object?  And therefore the .count property is undefined?

    How do I go about using the returned values without having to have one set of code for if an array is returned versus if only one item is returned?  Because I'm now getting errors with this type of code:

    if ( $imagefiles = Get-ChildItem $imagesfiles_filespec ) {
    Write-Host $imagefiles[0].name

    else { 

    Because Get-ChildItem isn't returning an array, I now get an error when I attempt to reference it as one.  Is there a generic way to handle the results whether it's one item returned or many?

    And thank-you very much for your help!


    • Edited by coupland Wednesday, June 15, 2016 1:38 AM
    Wednesday, June 15, 2016 1:32 AM
  • Thanks jrv, I used your code and it's now correctly handling a folder with a single file.  I assume this is because when there are two files Get-ChildItem is returning an array but if only one file exists it's returning a single object?  And therefore the .count property is undefined?

    How do I go about using the returned values without having to have one set of code for if an array is returned versus if only one item is returned?  Because I'm now getting errors with this type of code:

    if ( $imagefiles = Get-ChildItem $imagesfiles_filespec ) {
    Write-Host $imagefiles[0].name

    else { 

    Because Get-ChildItem isn't returning an array, I now get an error when I attempt to reference it as one.  Is there a generic way to handle the results whether it's one item returned or many?

    And thank-you very much for your help!


    Don't use count.  Just check the returned object with an "if".

    That is the power of PowerShell.


    \_(ツ)_/

    Wednesday, June 15, 2016 1:45 AM
  • $imagefiles = Get-ChildItem $filespec 
    if ($imagefiles.count -eq 1 -or $imagefiles.count -gt 1) { Write-Host Files found! $imagefiles.count }
    else { Write-Host Folder is empty! }
    
    or
    
    $imagefiles = Get-ChildItem $filespec 
    if ($imagefiles.count -ge 1) { Write-Host Files found! }
    
    else { Write-Host Folder is empty! 
    


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    • Proposed as answer by Hello_2018 Thursday, June 16, 2016 2:31 AM
    Wednesday, June 15, 2016 3:08 AM
  • Thanks jrv, I used your code and it's now correctly handling a folder with a single file.  I assume this is because when there are two files Get-ChildItem is returning an array but if only one file exists it's returning a single object?  And therefore the .count property is undefined?

    How do I go about using the returned values without having to have one set of code for if an array is returned versus if only one item is returned?  Because I'm now getting errors with this type of code:

    if ( $imagefiles = Get-ChildItem $imagesfiles_filespec ) {
    Write-Host $imagefiles[0].name

    else { 

    Because Get-ChildItem isn't returning an array, I now get an error when I attempt to reference it as one.  Is there a generic way to handle the results whether it's one item returned or many?

    And thank-you very much for your help!


    try:

    $xx=0
    [array]$imagefiles = Get-ChildItem $filespec 
    $i=$imagefiles.count
    
    if($imagefiles.count -gt 0){
    
     for  ($xx; $xx -lt $i; $xx++ ) {
    
    Write-Host "$($imagefiles.Count) Files found!"
    Write-Host   ($imagefiles[$xx].name  + $xx ) }
    
    }else{
    	Write-Host Folder is empty!
    }


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Wednesday, June 15, 2016 3:53 AM
  • Hi,

    In powershell 2 you should use @() operator when getting count property: 

    $imagefiles = Get-ChildItem $filespec 
     if (@($imagefiles).count -gt 0) { Write-Host Files found! }
     else { Write-Host Folder is empty! }
    Good luck)
    • Proposed as answer by Hello_2018 Thursday, June 16, 2016 2:31 AM
    • Marked as answer by Hello_2018 Friday, July 1, 2016 3:00 AM
    Wednesday, June 15, 2016 3:55 AM
  • Hi coupland,

    Thanks jrv, I used your code and it's now correctly handling a folder with a single file.  I assume this is because when there are two files Get-ChildItem is returning an array but if only one file exists it's returning a single object?  And therefore the .count property is undefined?

    How do I go about using the returned values without having to have one set of code for if an array is returned versus if only one item is returned?  Because I'm now getting errors with this type of code:

    if ( $imagefiles = Get-ChildItem $imagesfiles_filespec ) {
    Write-Host $imagefiles[0].name

    else { 

    Because Get-ChildItem isn't returning an array, I now get an error when I attempt to reference it as one.  Is there a generic way to handle the results whether it's one item returned or many?

    And thank-you very much for your help!


    Please try the following code:

    $t1 = get-childitem -path xxx

    $t1 | gm to find out name property

    $t1[0].name

    $t1[1].name

    Best regards,


    Andy_Pan

    Wednesday, June 15, 2016 8:33 AM
  • Besides jrv's solution, you can use

    $files = @(Get-Childitem $path)

    to always get an array, even if there is only one element. If there is only one element, the variable would be a simple object and not an array. And simple object have no count.

    As another solution, you can use Measure-Object cmdlet. 

    if( ($files | measure).Count -ge 1)
    {
    ...
    }
    Measure-Object returns 1, if you use it on a simple object like if it would be a one-elemental array.


    • Edited by hpotsirhc Wednesday, June 15, 2016 9:18 AM
    • Proposed as answer by Hello_2018 Thursday, June 16, 2016 2:31 AM
    Wednesday, June 15, 2016 9:18 AM
  • Thanks everyone!  You've been immensely helpful and my code is now working perfectly!  I really appreciate your assistance!  
    Wednesday, June 15, 2016 12:14 PM