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 - count number of files

Reply
 
Old 06-27-2008   #1 (permalink)


Vista Home Premium 32bit
 
 

count number of files

Hi guys. I need your help because I'm not able to solve a problem.

Let's suppose I have a folder like this

folder1
folder2
...
folderN
file1
file2
...
fileN

I want to count the number of files inside my folders, that is the total of files of folder1+folder2+folderN without counting file1,file2 and so on.
Thanks in advance.

My System SpecsSystem Spec
Old 06-27-2008   #2 (permalink)


Vista Home Premium 32bit
 
 

Re: count number of files

I've solved on my own.

$folders = gci | ? {-$_.psiscontainer}
$tot=0;foreach($folder in $folders) {$tot = $tot +(gci $folder).count}
$tot

I'm a newbie but I love powershell.
Thanks the same. Have a nice day.
My System SpecsSystem Spec
Old 06-27-2008   #3 (permalink)
Shay Levi


 
 

Re: count number of files

Hi sardinian_guy,


Glad you could resolve it yourself. I would like to add one thing, one Powershell
gotcha to be aware of:

In PowerShell, if your command results in one object only (scalar) then there
is no count property available. Therefor, trying to count
a directory that has only one child item will yield nothing. To make the
result (any result) have a count property, force it to array @(), as in:

@(gci $folder).count


That's a very important rule of thumb, remember it



BTW, if you want your command to include hidden files too, add the -force
parameter to gci.


---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

s> I've solved on my own.
s>
s> $folders = gci | ? {-$_.psiscontainer}
s> $tot=0;foreach($folder in $folders) {$tot = $tot +(gci
s> $folder).count}
s> $tot
s> I'm a newbie but I love powershell. Thanks the same. Have a nice day.
s>
s>


My System SpecsSystem Spec
Old 06-27-2008   #4 (permalink)


Vista Home Premium 32bit
 
 

Re: count number of files

Hi Shay.
I didn't know this thing.
I created a folder with just one file within and indeed it doesn't give me any result. If I add another file it returns correctly two. Thank you very much for this trick and for all your useful posts. I'm learning a lot of interesting things reading you and other great users of this forum.
Thanks again and sorry for my english.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Count number of times a box is ticked in Access 2003 Microsoft Office
Re: how to count number of certain char within string PowerShell
number of files in music folder Vista music pictures video
Count the number of Columns/rows in a CSV file.... 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