Discussion:
Counting items returned from Get-ChildItem....
(too old to reply)
fatboybubba
2007-03-05 13:52:17 UTC
Permalink
I'm not sure whats happening here, hopefully someone can explain. I
am trying to get the count of the number of items contained within a
folder but it appears that when there is only 1 item get-childitem
returns $null. Is there a better way to do this?

Clearly there is a file here:

PS T:\> gci T:\users\rdean -Recurse

Directory: Microsoft.PowerShell.Core\FileSystem::T:\users\rdean

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/8/2007 11:20 AM 619431 Poolmon3vbs.zip

But if I do the following the count property returns $null

PS T:\> (gci T:\users\rdean -Force).count
PS T:\>
PS T:\> if ((gci T:\users\rdean -Force).count -eq $null) { $true }
True

If I add a another item like the following the count property returns
2 like I would expect.

PS T:\> gci T:\users\rdean -Recurse

Directory: Microsoft.PowerShell.Core\FileSystem::T:\users\rdean

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/5/2007 8:43 AM New Folder
-a--- 2/8/2007 11:20 AM 619431 Poolmon3vbs.zip

PS T:\> (gci T:\users\rdean -Force).count
2
PS T:\>

Any ideas?

TIA

-Bob
Keith Hill
2007-03-05 15:15:56 UTC
Permalink
Post by fatboybubba
I'm not sure whats happening here, hopefully someone can explain. I
am trying to get the count of the number of items contained within a
folder but it appears that when there is only 1 item get-childitem
returns $null. Is there a better way to do this?
PS T:\> gci T:\users\rdean -Recurse
Directory: Microsoft.PowerShell.Core\FileSystem::T:\users\rdean
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/8/2007 11:20 AM 619431 Poolmon3vbs.zip
But if I do the following the count property returns $null
PS T:\> (gci T:\users\rdean -Force).count
In this scenario what's in the parens is a single System.IO.FileInfo object
that doens't have a Count property.
Post by fatboybubba
PS T:\>
PS T:\> if ((gci T:\users\rdean -Force).count -eq $null) { $true }
True
Same thing. No count property results in the condition evaluating to true.
Post by fatboybubba
If I add a another item like the following the count property returns
2 like I would expect.
PS T:\> gci T:\users\rdean -Recurse
Directory: Microsoft.PowerShell.Core\FileSystem::T:\users\rdean
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/5/2007 8:43 AM New Folder
-a--- 2/8/2007 11:20 AM 619431 Poolmon3vbs.zip
PS T:\> (gci T:\users\rdean -Force).count
2
OK so now that there are two files, what's in the parens is an array which
does have a count. To *ensure* that you always get an array result even if
there is only one or zero items contained in it, use the array subexpression
syntax:

@(gci T:\users\rdean -Force).count

Or a more PowerShell way to do this would be:

gci T:\users\rdean -force | measure-object

--
Keith
fatboybubba
2007-03-05 16:18:40 UTC
Permalink
Post by Keith Hill
Post by fatboybubba
I'm not sure whats happening here, hopefully someone can explain. I
am trying to get the count of the number of items contained within a
folder but it appears that when there is only 1 item get-childitem
returns $null. Is there a better way to do this?
PS T:\> gci T:\users\rdean -Recurse
Directory: Microsoft.PowerShell.Core\FileSystem::T:\users\rdean
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/8/2007 11:20 AM 619431 Poolmon3vbs.zip
But if I do the following the count property returns $null
PS T:\> (gci T:\users\rdean -Force).count
In this scenario what's in the parens is a single System.IO.FileInfo object
that doens't have a Count property.
Post by fatboybubba
PS T:\>
PS T:\> if ((gci T:\users\rdean -Force).count -eq $null) { $true }
True
Same thing. No count property results in the condition evaluating to true.
Post by fatboybubba
If I add a another item like the following the count property returns
2 like I would expect.
PS T:\> gci T:\users\rdean -Recurse
Directory: Microsoft.PowerShell.Core\FileSystem::T:\users\rdean
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/5/2007 8:43 AM New Folder
-a--- 2/8/2007 11:20 AM 619431 Poolmon3vbs.zip
PS T:\> (gci T:\users\rdean -Force).count
2
OK so now that there are two files, what's in the parens is an array which
does have a count. To *ensure* that you always get an array result even if
there is only one or zero items contained in it, use the array subexpression
@(gci T:\users\rdean -Force).count
gci T:\users\rdean -force | measure-object
--
Keith- Hide quoted text -
- Show quoted text -
Good Stuff

Thank you.

Loading...