![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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> |
| | #2 (permalink) |
| 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 |
| | #3 (permalink) | ||||||||||||
| 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
| ||||||||||||
| | #4 (permalink) | ||||||||||||
| Guest
Posts: n/a
| Re: Count bug in Get_ChildItem? "Byron" <Byron@xxxxxx> wrote in message news:9BDBC83C-6719-42E6-BD38-2FB172F1CFD0@xxxxxx
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 | ||||||||||||
| |
| |
![]() |
| 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 |