![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Retrieving file count from a list of child folders All, I am fighting some other fires this morning, and hoping for a quick assist. I need to do file counts on all the subfolders in a target folder. For a single forlder, this works (Get-ChildItem C:\Scripts).Count but when I add recurse, it simply returns a single filecount for all child folders combined. (Get-ChildItem C:\Scripts -recurse).Count How would I modify this to return file counts for individual child folders? Regards, Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - Opsmgr URL:http://www.systemcenterforum.org User Group: http://www.systemcenterusergroup.com MP Catalog: http://www.systemcenterforum.org/mps Tools: http://www.systemcenterforum.org/tools/ |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Retrieving file count from a list of child folders (Get-ChildItem C:\Scripts).Count - returns file and folder count. try this: dir | ?{$_.psiscontainer} | %{@{$_.name=(dir $_ | ?{$_.psiscontainer -eq $false}).count}} enjoy! -- WBR, Vadims Podans PowerShell blog - www.sysadmins.lv "Pete Zerger (MVP)" <pete.zerger@xxxxxx> rakstīja ziņojumā "news:265ff311130be8cb558f6a83feaa@xxxxxx"... Quote: > All, > I am fighting some other fires this morning, and hoping for a quick > assist. > > I need to do file counts on all the subfolders in a target folder. > For a single forlder, this works > (Get-ChildItem C:\Scripts).Count > > but when I add recurse, it simply returns a single filecount for all child > folders combined. > (Get-ChildItem C:\Scripts -recurse).Count > > > How would I modify this to return file counts for individual child > folders? > > Regards, > > Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - > Opsmgr > URL:http://www.systemcenterforum.org > User Group: http://www.systemcenterusergroup.com > MP Catalog: http://www.systemcenterforum.org/mps > Tools: http://www.systemcenterforum.org/tools/ > > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Retrieving file count from a list of child folders You could also try this $fso = New-Object -com "Scripting.FileSystemObject" $f = $fso.GetFolder("C:\scripts") foreach ($folder in $f.subfolders){Write-Host $folder.Path $((get-childitem $folder.path).count)} -- Richard Siddaway All scripts are supplied "as is" and with no warranty PowerShell MVP Blog: http://richardsiddaway.spaces.live.com/ PowerShell User Group: http://www.get-psuguk.org.uk "Vadims Podans" wrote: Quote: > (Get-ChildItem C:\Scripts).Count - returns file and folder count. > > try this: > dir | ?{$_.psiscontainer} | %{@{$_.name=(dir $_ | ?{$_.psiscontainer -eq > $false}).count}} > > enjoy! > -- > WBR, Vadims Podans > PowerShell blog - www.sysadmins.lv > > "Pete Zerger (MVP)" <pete.zerger@xxxxxx> rakstīja ziņojumā > "news:265ff311130be8cb558f6a83feaa@xxxxxx"... Quote: > > All, > > I am fighting some other fires this morning, and hoping for a quick > > assist. > > > > I need to do file counts on all the subfolders in a target folder. > > For a single forlder, this works > > (Get-ChildItem C:\Scripts).Count > > > > but when I add recurse, it simply returns a single filecount for all child > > folders combined. > > (Get-ChildItem C:\Scripts -recurse).Count > > > > > > How would I modify this to return file counts for individual child > > folders? > > > > Regards, > > > > Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - > > Opsmgr > > URL:http://www.systemcenterforum.org > > User Group: http://www.systemcenterusergroup.com > > MP Catalog: http://www.systemcenterforum.org/mps > > Tools: http://www.systemcenterforum.org/tools/ > > > > |
My System Specs![]() |
| | #4 (permalink) |
| | RE: Retrieving file count from a list of child folders Another way, just for the fun of it. Get-ChildItem -recurse | Where {!$_.PSIsContainer} | Group Directory | Format-Table Name, Count -autosize "Pete Zerger (MVP)" wrote: Quote: > All, > > I am fighting some other fires this morning, and hoping for a quick assist. > > I need to do file counts on all the subfolders in a target folder. > > For a single forlder, this works > (Get-ChildItem C:\Scripts).Count > > but when I add recurse, it simply returns a single filecount for all child > folders combined. > (Get-ChildItem C:\Scripts -recurse).Count > > > How would I modify this to return file counts for individual child folders? > > > Regards, > > Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - Opsmgr > URL:http://www.systemcenterforum.org > User Group: http://www.systemcenterusergroup.com > MP Catalog: http://www.systemcenterforum.org/mps > Tools: http://www.systemcenterforum.org/tools/ > > > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Retrieving file count from a list of child folders Hello Pete, Three great answers to only one question. My expectations are exceeded on every visit to this newsgroup. Keep up the great work! Regards, Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - Opsmgr URL:http://www.systemcenterforum.org User Group: http://www.systemcenterusergroup.com MP Catalog: http://www.systemcenterforum.org/mps Tools: http://www.systemcenterforum.org/tools/ Quote: > All, > > I am fighting some other fires this morning, and hoping for a quick > assist. > > I need to do file counts on all the subfolders in a target folder. > > For a single forlder, this works > (Get-ChildItem C:\Scripts).Count > but when I add recurse, it simply returns a single filecount for all > child > folders combined. > (Get-ChildItem C:\Scripts -recurse).Count > How would I modify this to return file counts for individual child > folders? > > Regards, > > Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - > Opsmgr > URL:http://www.systemcenterforum.org > User Group: http://www.systemcenterusergroup.com > MP Catalog: http://www.systemcenterforum.org/mps > Tools: http://www.systemcenterforum.org/tools/ |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Retrieving file count from a list of child folders Hello Pete, Quote: > Hello Pete, > > Three great answers to only one question. My expectations are exceeded > on every visit to this newsgroup. Keep up the great work! > > Regards, > > Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - > Opsmgr > URL:http://www.systemcenterforum.org > User Group: http://www.systemcenterusergroup.com > MP Catalog: http://www.systemcenterforum.org/mps > Tools: http://www.systemcenterforum.org/tools/ Quote: >> All, >> >> I am fighting some other fires this morning, and hoping for a quick >> assist. >> >> I need to do file counts on all the subfolders in a target folder. >> >> For a single forlder, this works >> (Get-ChildItem C:\Scripts).Count >> but when I add recurse, it simply returns a single filecount for all >> child >> folders combined. >> (Get-ChildItem C:\Scripts -recurse).Count >> How would I modify this to return file counts for individual child >> folders? >> Regards, >> >> Pete Zerger, MCSE(Messaging) | MCTS(SQL 2005) | MCTS(Opsmgr) | MVP - >> Opsmgr >> URL:http://www.systemcenterforum.org >> User Group: http://www.systemcenterusergroup.com >> MP Catalog: http://www.systemcenterforum.org/mps >> Tools: http://www.systemcenterforum.org/tools/ |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Retrieving file count from a list of child folders PaulChavez <PaulChavez@xxxxxx> wrote: Quote: >Another way, just for the fun of it. > >Get-ChildItem -recurse | Where {!$_.PSIsContainer} | Group Directory | >Format-Table Name, Count -autosize script and got some mysterious shell error when I swapped the code of solution 2 with solution 1. Both solutions work well for themself. Could anybody explain the error message to me? Powershell says something that format-table caused a problem with the standard formatting. ---------- cut here ------------ # this script works as expected when solution 2 is called before solution 1 # but there exists some shell error when solution 1 is called before solution 2. Why? cls $mypath="C:\tmp" #solution 2 Get-ChildItem -recurse $mypath | Where {!$_.PSIsContainer} | Group-Object Directory |Format-Table Name, Count -autosize #solution 1 Get-ChildItem -recurse $mypath | Where {$_.psiscontainer} |Foreach { @{ $_.Name = (dir $_.FullName | Where { $_.psiscontainer -eq $false} ).count}} ---------- cut here ------------ Thanks for any explanation. Thorsten |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Retrieving file count from a list of child folders What's happening is that the output of both commands are being combined (they're executed within a script) and neither, the outputter nor the formatter, don't know how to handle the second stream, because it's expecting a specific set of format elements but the second stream emits different ones that are incompatible with the first set. Usually, PowerShell handles this by delivering the output as a List, but not when the initial stream is emitted as a Table. As you already discover swapping the order of the commands can resolve the problem, but it's easier to separate the streams. To separate the streams you can _pipe_ the first one to a) Out-Default if you want to write/capture it, or b) Write-Host if you just want it displayed. # reproduce the behavior in the console by executing both commands # in one line at a time # PS outputs the 2nd stream as a List @{key = 'val1'}; get-process # 1st stream as a HashTable, 2nd as a Table ...PS chokes @{key = 'val1'}; get-process | ft # 1st stream as a HashTable, 2nd as a List ...PS chokes @{key = 'val1'}; get-process | fl # separate the streams with Out-Default # PS outputs both streams fine @{key = 'val1'} | out-default; get-process @{key = 'val1'} | out-default; get-process | ft # 1st stream as a Table to Out-Default, # 2nd as a List ...no problem @{key = 'val1'} | out-default; get-process | fl -- Kiron |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Retrieving file count from a list of child folders "Kiron" <Kiron@xxxxxx> wrote: Quote: >To separate the streams you can _pipe_ the first one to a) Out-Default if you want to write/capture it, or b) Write-Host if you just want it displayed. Thorsten |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Retrieving VersionInfo for a list of file | PowerShell | |||
| Folders Not Updating Unread Message Count | Live Mail | |||
| BUG: Incorrect message count in deleted and junk folders | Live Mail | |||
| why do i have more pictures folders than i can count and how do i | Vista performance & maintenance | |||
| How to count the total files under c:\abc\<* folders>\out where <* folders> are many folders? | PowerShell | |||