# Re: how to calculate file number in a folder

**URL:** <https://www.ruby-forum.com/t/re-how-to-calculate-file-number-in-a-folder/73047>\
**Category:** Ruby\
**Created:** [October 20, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/re-how-to-calculate-file-number-in-a-folder/73047 "2006-10-20T21:17:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/re-how-to-calculate-file-number-in-a-folder/73047/1 "2006-10-20T21:17:16Z")

</div>

From: Li Chen

> but I am not sure how to calculate the number of file in folder.  
> Any help will be appreciated.

Here’s one way. (There are many, I’m sure.)

#Find all the entries in a directory  
irb(main):009:0\> Dir[‘c:/ruby/\*’]  
=\> [“c:/ruby/bin”, “c:/ruby/ChangeLog.txt”, “c:/ruby/doc”,  
“c:/ruby/freeride”, “c:/ruby/lib”, “c:/ruby/LICENSE.txt”, “c:/ruby/man”,  
“c:/ruby/MANIFEST”, “c:/ruby/README.1st”, “c:/ruby/ReleaseNotes.txt”,  
“c:/ruby/ruby.ico”, “c:/ruby/rubyw.ico”, “c:/ruby/samples”,  
“c:/ruby/scite”, “c:/ruby/share”, “c:/ruby/src”,  
“c:/ruby/uninstall.exe”]

# Find select just the ones that are files:

irb(main):010:0\> Dir[‘c:/ruby/\*’].select{ |path| File.file?( path ) }  
=\> [“c:/ruby/ChangeLog.txt”, “c:/ruby/LICENSE.txt”, “c:/ruby/MANIFEST”,  
“c:/ruby/README.1st”, “c:/ruby/ReleaseNotes.txt”, “c:/ruby/ruby.ico”,  
“c:/ruby/rubyw.ico”, “c:/ruby/uninstall.exe”]

# Find the length of that list

irb(main):011:0\> Dir[‘c:/ruby/\*’].select{ |path| File.file?( path )  
}.length  
=\> 8

One other way:

file\_count = 0  
Dir[‘c:/ruby/\*’].each{ |path|  
if File.file?( path )  
# Do something to the file  
file\_count += 1  
end  
}

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 20, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/re-how-to-calculate-file-number-in-a-folder/73047/2 "2006-10-20T22:09:12Z")

</div>

> Here’s one way. (There are many, I’m sure.)
> 
> #Find all the entries in a directory  
> irb(main):009:0\> Dir[‘c:/ruby/\*’]
> 
> # Find select just the ones that are files:
> 
> irb(main):010:0\> Dir[‘c:/ruby/\*’].select{ |path| File.file?( path ) }

> # Find the length of that list
> 
> irb(main):011:0\> Dir[‘c:/ruby/\*’].select{ |path| File.file?( path )  
> }.length  
> =\> 8
> 
> One other way:
> 
> file\_count = 0  
> Dir[‘c:/ruby/\*’].each{ |path|  
> if File.file?( path )  
> # Do something to the file  
> file\_count += 1  
> end  
> }

Thank you,

Li
