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 - Retrieving file count from a list of child folders

Reply
 
Old 02-05-2009   #1 (permalink)
Pete Zerger (MVP)


 
 

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 SpecsSystem Spec
Old 02-05-2009   #2 (permalink)
Vadims Podans


 
 

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 SpecsSystem Spec
Old 02-05-2009   #3 (permalink)
RichS [MVP]


 
 

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 SpecsSystem Spec
Old 02-05-2009   #4 (permalink)
PaulChavez


 
 

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 SpecsSystem Spec
Old 02-05-2009   #5 (permalink)
Pete Zerger (MVP)


 
 

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 SpecsSystem Spec
Old 02-05-2009   #6 (permalink)
Karl Mitschke


 
 

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/
I hate to ask, but are you talking to yourself, or are you just schizophrenic?


My System SpecsSystem Spec
Old 03-18-2009   #7 (permalink)
Thorsten Albrecht


 
 

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
When trying the solutions proposed above, I invoked the following
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 SpecsSystem Spec
Old 03-20-2009   #8 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Old 03-23-2009   #9 (permalink)
Thorsten Albrecht


 
 

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.
Ok, thank you for your explanation.

Thorsten
My System SpecsSystem Spec
Reply

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


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