# Outputting file sizes of each item in a directory (error)

**URL:** <https://www.ruby-forum.com/t/outputting-file-sizes-of-each-item-in-a-directory-error/120032>\
**Category:** Ruby\
**Created:** [November 13, 2007, 6:59am UTC](https://www.ruby-forum.com/t/outputting-file-sizes-of-each-item-in-a-directory-error/120032 "2007-11-13T06:59:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 13, 2007, 6:59am UTC](https://www.ruby-forum.com/t/outputting-file-sizes-of-each-item-in-a-directory-error/120032/1 "2007-11-13T06:59:44Z")

</div>

code:

# Dir.foreach(“bad”) do |f| puts “#{f}: #{File.size f}” end

Each filename in the directory “bad” is displayed along with the file  
size.

I know the file.size works because I tried it with an static name and  
displayed fine.

the error I got

# ============ lib/dir\_test.rb:6:in `size': No such file or directory - IMG_0964.jpg (Errno::ENOENT) from lib/dir_test.rb:6 from lib/dir_test.rb:5:in`foreach’ from lib/dir\_test.rb:5

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 13, 2007, 7:14am UTC](https://www.ruby-forum.com/t/outputting-file-sizes-of-each-item-in-a-directory-error/120032/2 "2007-11-13T07:14:14Z")

</div>

On 13 Nov 2007, at 14:59, Feng T. wrote:

> # I know the file.size works because I tried it with an static name and

Add ‘bad/’ to each filename. E.g:

Dir.foreach(“bad”) do |f|  
puts “#{f}: #{File.size(‘bad/’+f)}”  
end

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 13, 2007, 7:41am UTC](https://www.ruby-forum.com/t/outputting-file-sizes-of-each-item-in-a-directory-error/120032/3 "2007-11-13T07:41:07Z")

</div>

Alex G. wrote:

> On 13 Nov 2007, at 14:59, Feng T. wrote:
> 
> > # I know the file.size works because I tried it with an static name and
> 
> Add ‘bad/’ to each filename. E.g:
> 
> Dir.foreach(“bad”) do |f|  
> puts “#{f}: #{File.size(‘bad/’+f)}”  
> end
> 
> Alex G.
> 
> Bioinformatics Center  
> Kyoto University

excellent, thanks it worked!

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 13, 2007, 8:36am UTC](https://www.ruby-forum.com/t/outputting-file-sizes-of-each-item-in-a-directory-error/120032/4 "2007-11-13T08:36:07Z")

</div>

Quoth Feng T.:

> > Alex G.
> > 
> > Bioinformatics Center  
> > Kyoto University
> 
> excellent, thanks it worked!

If you did:

Dir[“bad/\*”].each do |f|  
…  
end

in the first place, you wouldn’t need to add the ‘bad/’. I was  
originally  
going to reply to suggest adding it, but then I though .foreach() would  
do  
that for ya. Guess not.

Regards,
