Reading files from dir

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) %>
[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.

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.

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.

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