Removing file names with '.' in their names from list?

  • Thread starter Sfdesigner Sfdesigner
  • Start date
S

Sfdesigner Sfdesigner

Currently I'm using this code to output a list of files to a .rhtml
page:

<%
Dir.foreach("/") do |file|
next if File.fnmatch('*.*', file)
puts '<a href="file">' + file + '</a>'
end
%>

This removes files from the list output such as 'photo.jpg'.

But it leaves the files with names like '.htaccess', ',' and '..'.

I could create more of the 'next' statements explicitly for those file
and directory names but wondering if there's a more elegant way to say
don't output files with a '.' in their names.
 
R

Robert Dober

Currently I'm using this code to output a list of files to a .rhtml
page:

<%
Dir.foreach("/") do |file|
next if File.fnmatch('*.*', file)
I would suggest
next if /\./ === file
puts '<a href="file">' + file + '</a>'
end
%>
HTH
Robert
 
K

Ken Bloom

Currently I'm using this code to output a list of files to a .rhtml
page:

<%
Dir.foreach("/") do |file|
next if File.fnmatch('*.*', file)
puts '<a href="file">' + file + '</a>'
end
%>

This removes files from the list output such as 'photo.jpg'.

But it leaves the files with names like '.htaccess', ',' and '..'.

I could create more of the 'next' statements explicitly for those file
and directory names but wondering if there's a more elegant way to say
don't output files with a '.' in their names.

Under UNIX, a * never matches a . at the beginning of a filename, and *.*
also never matches this.

add another rule
next if File.fnmatch('.*',file)
and you should be fine.
 
N

Nobuyoshi Nakada

Hi,

At Mon, 13 Aug 2007 07:16:49 +0900,
Sfdesigner Sfdesigner wrote in [ruby-talk:264328]:
Currently I'm using this code to output a list of files to a .rhtml
page:

<%
Dir.foreach("/") do |file|
next if File.fnmatch('*.*', file)
puts '<a href="file">' + file + '</a>'
end
%>

This removes files from the list output such as 'photo.jpg'.

But it leaves the files with names like '.htaccess', ',' and '..'.

File.fnmatch('*.*', file, File::FNM_DOTMATCH)
 
C

Chris Shea

Currently I'm using this code to output a list of files to a .rhtml
page:

<%
Dir.foreach("/") do |file|
next if File.fnmatch('*.*', file)
puts '<a href="file">' + file + '</a>'
end
%>

This removes files from the list output such as 'photo.jpg'.

But it leaves the files with names like '.htaccess', ',' and '..'.

I could create more of the 'next' statements explicitly for those file
and directory names but wondering if there's a more elegant way to say
don't output files with a '.' in their names.

perhaps:
next if file.include?('.')

Though Robert's solution does the same thing.

HTH,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top