Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Count bug in Get_ChildItem?

Reply
 
Old 09-04-2007   #1 (permalink)
Byron


 
 

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>

My System SpecsSystem Spec
Old 09-04-2007   #2 (permalink)
Kiron


 
 

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

My System SpecsSystem Spec
Old 09-04-2007   #3 (permalink)
Jon


 
 

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>
My System SpecsSystem Spec
Old 09-04-2007   #4 (permalink)
Keith Hill


 
 

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

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Get-childitem count bug or not bug ? PowerShell
count PowerShell
arguments count PowerShell
Message count Vista mail
Count PowerShell


Vista Forums 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 Ltd

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