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