# Reading files from dir

**URL:** <https://www.ruby-forum.com/t/reading-files-from-dir/72127>\
**Category:** Rails\
**Created:** [February 23, 2006, 12:52am UTC](https://www.ruby-forum.com/t/reading-files-from-dir/72127 "2006-02-23T00:52:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [February 23, 2006, 12:52am UTC](https://www.ruby-forum.com/t/reading-files-from-dir/72127/1 "2006-02-23T00:52:57Z")

</div>

I have gif files in my public/images/posticons directory, I want to read  
their filenames without the extension to list the files as a set of  
radio buttons in the form:

[code]\<%= radio\_button(“thread”, “posticon”, imgname) %\>  
 ![](https://www.ruby-forum.com/images/posticons/<%=%20imgname%>.gif)[code]

Would I be saving myself a lot of time and server resources by just  
saving all of the posticons into the database instead? It’s pretty  
convenient being able to just put and change the post icons by adding  
and removing files from this directory.

There is also one icon called default.gif that I don’t want to list and  
only use it when there is no other icon chosen. I can do that myself I’m  
just having troubles getting all the files to show up from this  
directory.

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [February 26, 2006, 7:18am UTC](https://www.ruby-forum.com/t/reading-files-from-dir/72127/2 "2006-02-26T07:18:16Z")

</div>

> I have gif files in my public/images/posticons directory, I want to read  
> their filenames without the extension to list the files as a set of  
> radio buttons in the form:
> 
> There is also one icon called default.gif that I don’t want to list and  
> only use it when there is no other icon chosen. I can do that myself I’m  
> just having troubles getting all the files to show up from this  
> directory.

Nathan,

Did you have any luck with this?

Did you end up using the Dir and/or File classes (in the core Ruby  
library)?

I need to do something similar but can’t find any code examples.

Regards,  
Estelle.

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [March 13, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/reading-files-from-dir/72127/3 "2008-03-13T21:11:02Z")

</div>

Estelle W. wrote:

> > I have gif files in my public/images/posticons directory, I want to read  
> > their filenames without the extension to list the files as a set of  
> > radio buttons in the form:
> > 
> > There is also one icon called default.gif that I don’t want to list and  
> > only use it when there is no other icon chosen. I can do that myself I’m  
> > just having troubles getting all the files to show up from this  
> > directory.

Here’s what I’ve done below (“myImages” is a subdirectory of the  
“images/” directory of public) and it works just fine as long as you are  
currently in the directory that has the files or in the parent of it. I  
haven’t used it in my app from a view, just through the IRB console.

d = Dir.entries(“myImages”)  
d.foreach do |f|  
filename = f.delete(f.slice(/…/))  
end

That removes the “.ext” from the string and you’re left with just the  
filename.

You could use the File I/O methods to read the attributes of the file  
but why bother when this works too.

---

<div class="post-metadata">

**Author:** ![pie-social](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/aca169/32.png) [@pie-social](https://www.ruby-forum.com/u/pie-social)\
**Post date:** [March 13, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/reading-files-from-dir/72127/4 "2008-03-13T21:12:20Z")

</div>

> d = Dir.entries(“myImages”)  
> d.foreach do |f|  
> filename = f.delete(f.slice(/…/))  
> end

Correction see below…

d = Dir.entries(“myImages”)  
d.each do |f|  
filename = f.delete(f.slice(/…/))  
end
