arrayspowershellmember-enumeration

Get-ChildItem.Length is Wrong


I am writing a recursive function that goes through a directory and copies every file and folder in it. The first check I have in the function is to see if the path passed in has children. To find this out, I use the following method:

[array]$arrExclude = @("Extras")
Function USBCopy
{
Param ([string]$strPath, [string]$strDestinationPath)
    try
    {
        $pathChildren = Get-ChildItem -Path $strPath
        if($pathChildren.Length -gt 0)
        {
            foreach($child in $pathChildren)
            {
                if($arrExclude -notcontains $child)
                {
                    $strPathChild = "$strPath\$child"
                    $strDestinationPathChild = "$strDestinationPath\$child" 
                    Copy-Item $strPathChild -Destination $strDestinationPathChild
                    USBCopy $strPathChild $strDestinationPathChild  
                }   
            }
        }              
    }
    catch
    {
        Write-Error ("Error running USBCopy: " + $Error[0].Exception.Message)
    }    
}

For the most part my function works, but my code will say a directory is empty when it actually has 1 file in it. When I debug my function, the variable will say that the folder has children but the variable's length is 0. Anyone know how to get around this?


Solution

  • Try $pathChildren.Count instead of $pathChildren.Length - that will return the number of items in the array.