![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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. 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 Specs![]() |
![]() |
| 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 | |||