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 - Counting files in a directory

Reply
 
Old 04-04-2007   #1 (permalink)
Jim


 
 

Counting files in a directory

I just noticed that get-childitem (dir, ls) does not return the number
of files shown.

One way is to use (ls splat*).count, but that's not exactly what 'ls'
does. Is there a built in way to do this? Or, should I keep using
the filter I wrote, below?

filter sum($prop="Length") {
begin { $count=0; $total=0 }
process{ $_; $count++; $total+=$_.$prop }
end{ Write-Output ("`nCount: $count, Total: $total, Avg: " + $total/
$count) }
}

Example usage:

ls splat* | sum


Also, is there an easy way to format the numbers as bytes? "233K" or
"12.30M" for example? Even comma insertion would be helpful.


My System SpecsSystem Spec
Old 04-04-2007   #2 (permalink)
Brandon Shell


 
 

Re: Counting files in a directory

To get count of the files/folders in a Directory:
(get-childitem $directory).Count

For just Files
(get-childitem $directory | ?{!($_.PSIsContainer)}).Count

For just Folders
(get-childitem $directory | ?{$_.PSIsContainer}).Count

Now... for recursive just add -recurse... but beware, could take awhile

As for the conversion (this assumes number is bytes.)
$number/1KB
$number/1MB
$number/1GB

Here some info on it
http://blogs.msdn.com/powershell/arc...4-vs-1000.aspx

--
Brandon Shell
---------------
Stop by my blog some time
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
--------------------------------------

"Jim" <james_hugard@hotmail.com> wrote in message
news:1175717228.912187.177270@e65g2000hsc.googlegroups.com...
>I just noticed that get-childitem (dir, ls) does not return the number
> of files shown.
>
> One way is to use (ls splat*).count, but that's not exactly what 'ls'
> does. Is there a built in way to do this? Or, should I keep using
> the filter I wrote, below?
>
> filter sum($prop="Length") {
> begin { $count=0; $total=0 }
> process{ $_; $count++; $total+=$_.$prop }
> end{ Write-Output ("`nCount: $count, Total: $total, Avg: " + $total/
> $count) }
> }
>
> Example usage:
>
> ls splat* | sum
>
>
> Also, is there an easy way to format the numbers as bytes? "233K" or
> "12.30M" for example? Even comma insertion would be helpful.
>


My System SpecsSystem Spec
Old 04-04-2007   #3 (permalink)
Keith Hill


 
 

Re: Counting files in a directory

"Jim" <james_hugard@hotmail.com> wrote in message
news:1175717228.912187.177270@e65g2000hsc.googlegroups.com...
>I just noticed that get-childitem (dir, ls) does not return the number
> of files shown.
>
> One way is to use (ls splat*).count, but that's not exactly what 'ls'
> does. Is there a built in way to do this? Or, should I keep using
> the filter I wrote, below?
>
> filter sum($prop="Length") {
> begin { $count=0; $total=0 }
> process{ $_; $count++; $total+=$_.$prop }
> end{ Write-Output ("`nCount: $count, Total: $total, Avg: " + $total/
> $count) }
> }


dir | measure-object -property length -sum -ave

would be a bit more straight forward.

If you have PowerShell Community Extensions installed then you can do this:

13> dir | measure-object -property length -sum -ave |
fl @{l='Sum';e={format-byte $_.Sum}}, @{l='Average';e={format-byte
$_.Average}}


Sum : 597.059 KB
Average : 20.588 KB

You can download PSCX from http://www.codeplex.com/powershellcx

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Counting Files and Folders General Discussion
How to list all files under a directory? Vista General
Move files to a directory PowerShell
rename all files from a directory to a list of files ... PowerShell
Copying files to Program Files directory Vista file management


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