Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Count bug in Get_ChildItem?

Closed Thread
 
Thread Tools Display Modes
Old 09-04-2007   #1 (permalink)
Byron
Guest
 
Posts: n/a

Count bug in Get_ChildItem?

The output below illustrates what appears to be a bug in PowerShell. I am
trying to identify empty folders by testing the Count of found files. It
appears that there must be 2 or more files in the current folder before Count
contains a value. Has anyone else seen this, or can you explain why Count
apparently never equals 1?

I started with one file in the folder and $Files.Count returned nothing.
When I added a file $Files.Count went to 2.

----------------

PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem


Directory:
Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/4/2007 10:46 AM 942 a.txt


PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files = get-childitem
PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files


Directory:
Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/4/2007 10:46 AM 942 a.txt


PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files.Count
*** NO COUNT OUTPUT ***
PS C:\WINDOWS\system32\windowspowershell\v1.0\x> dir > b.txt
PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem


Directory:
Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/4/2007 10:46 AM 942 a.txt
-a--- 9/4/2007 10:49 AM 1184 b.txt


PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files = get-childitem
PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files


Directory:
Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/4/2007 10:46 AM 942 a.txt
-a--- 9/4/2007 10:49 AM 1184 b.txt


PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files.Count
2
PS C:\WINDOWS\system32\windowspowershell\v1.0\x>
 
Old 09-04-2007   #2 (permalink)
Kiron
Guest
 
Posts: n/a

Re: Count bug in Get_ChildItem?

In an empty folder get-childItem's return is $null.
In a folder with one item the object get-childItem returns is either
System.IO.DirectoryInfo or System.IO.FileInfo depending if the item is a
folder or a file. Neither object has a Count property; System.IO.FileInfo
has a Length property but that contains the files size.
In a folder with more than one item get-childItem returns a System.Object[]
array which has the Count property.
To ensure a Count property in any scenario enclose the expression with the
array notation '@()', that way you will always get a System.Object[].

$Files = @(get-childitem)
$Files.count

--
Kiron

 
Old 09-04-2007   #3 (permalink)
Jon
Guest
 
Posts: n/a

Re: Count bug in Get_ChildItem?

It's because when you have 2 files, $files is an array containing 2 files,
whereas it's a 'fileinfo' object with just 1 file.

Try this with your 2 examples
$files.GetType()

This should work for both cases
$Files = @(get-childitem)

--
Jon


"Byron" <Byron@xxxxxx> wrote in message
news:9BDBC83C-6719-42E6-BD38-2FB172F1CFD0@xxxxxx
Quote:

> The output below illustrates what appears to be a bug in PowerShell. I am
> trying to identify empty folders by testing the Count of found files. It
> appears that there must be 2 or more files in the current folder before
> Count
> contains a value. Has anyone else seen this, or can you explain why Count
> apparently never equals 1?
>
> I started with one file in the folder and $Files.Count returned nothing.
> When I added a file $Files.Count went to 2.
>
> ----------------
>
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem
>
>
> Directory:
> Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x
>
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 9/4/2007 10:46 AM 942 a.txt
>
>
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files = get-childitem
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files
>
>
> Directory:
> Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x
>
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 9/4/2007 10:46 AM 942 a.txt
>
>
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files.Count
> *** NO COUNT OUTPUT ***
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> dir > b.txt
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem
>
>
> Directory:
> Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x
>
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 9/4/2007 10:46 AM 942 a.txt
> -a--- 9/4/2007 10:49 AM 1184 b.txt
>
>
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files = get-childitem
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files
>
>
> Directory:
> Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\system32\windowspowershell\v1.0\x
>
>
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 9/4/2007 10:46 AM 942 a.txt
> -a--- 9/4/2007 10:49 AM 1184 b.txt
>
>
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x> $Files.Count
> 2
> PS C:\WINDOWS\system32\windowspowershell\v1.0\x>
 
Old 09-04-2007   #4 (permalink)
Keith Hill
Guest
 
Posts: n/a

Re: Count bug in Get_ChildItem?

"Byron" <Byron@xxxxxx> wrote in message
news:9BDBC83C-6719-42E6-BD38-2FB172F1CFD0@xxxxxx
Quote:

> The output below illustrates what appears to be a bug in PowerShell. I am
> trying to identify empty folders by testing the Count of found files. It
> appears that there must be 2 or more files in the current folder before
> Count
> contains a value. Has anyone else seen this, or can you explain why Count
> apparently never equals 1?
>
> I started with one file in the folder and $Files.Count returned nothing.
> When I added a file $Files.Count went to 2.
Classic nuance of PowerShell. When a cmdlet only outputs a single item, you
are dealing with a scalar value. In this case a System.IO.FileInfo object
and it has no count property. As soon as you have multiple items and assign
that to a variable, you get an array. Array's in PowerShell do have a count
property. There are two ways to work with this. First is to use the
measure-object cmdlet - it handles this situation very well:

PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem |
measure-object

The second way is to force the result into an array like so:

$Files = @(get-childitem)
$Files.count
1

--
Keith

 
 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
count IT STAFF PowerShell 7 07-16-2008 04:52 AM
count does not work less than 2 Frank PowerShell 2 11-11-2007 01:40 PM
Count Thomas PowerShell 3 03-02-2007 01:23 AM
Bug in array.count? Msh newbie PowerShell 5 02-08-2007 05:59 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49