[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Sfdesigner Sfdesigner

8/12/2007 10:17:00 PM

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.
--
Posted via http://www.ruby-....

5 Answers

Robert Dober

8/12/2007 11:19:00 PM

0

On 8/12/07, Sfdesigner Sfdesigner <daniel@seniorartdirector.com> wrote:
> 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
--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

Sfdesigner Sfdesigner

8/12/2007 11:49:00 PM

0

> I would suggest
> next if /\./ === file

Beautiful. Thanks!
--
Posted via http://www.ruby-....

Ken Bloom

8/13/2007 12:38:00 AM

0

On Mon, 13 Aug 2007 07:16:49 +0900, Sfdesigner Sfdesigner wrote:

> 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.

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Nobuyoshi Nakada

8/13/2007 2:38:00 AM

0

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)

--
Nobu Nakada

Chris Shea

8/13/2007 2:38:00 AM

0

On Aug 12, 4:16 pm, Sfdesigner Sfdesigner
<dan...@seniorartdirector.com> wrote:
> 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.
> --
> Posted viahttp://www.ruby-....

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

Though Robert's solution does the same thing.

HTH,
Chris