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

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 ‘’ + file + ‘
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.

On 8/12/07, Sfdesigner S. [email protected] 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 ‘’ + file + ‘
end
%>

HTH
Robert

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

Beautiful. Thanks!

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

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.

Hi,

At Mon, 13 Aug 2007 07:16:49 +0900,
Sfdesigner S. wrote in [ruby-talk:264328]:

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)

On Aug 12, 4:16 pm, Sfdesigner S.
[email protected] wrote:

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-forum.com/.

perhaps:
next if file.include?(‘.’)

Though Robert’s solution does the same thing.

HTH,
Chris