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

**URL:** <https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885>\
**Category:** Ruby\
**Created:** [August 13, 2007, 12:16am UTC](https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885 "2007-08-13T00:16:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [August 13, 2007, 12:16am UTC](https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885/1 "2007-08-13T00:16:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [August 13, 2007, 1:19am UTC](https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885/2 "2007-08-13T01:19:47Z")

</div>

On 8/12/07, Sfdesigner S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [August 13, 2007, 1:49am UTC](https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885/3 "2007-08-13T01:49:02Z")

</div>

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

Beautiful. Thanks!

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [August 13, 2007, 2:41am UTC](https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885/4 "2007-08-13T02:41:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [August 13, 2007, 4:38am UTC](https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885/5 "2007-08-13T04:38:59Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [August 13, 2007, 4:40am UTC](https://www.ruby-forum.com/t/removing-file-names-with-in-their-names-from-list/111885/6 "2007-08-13T04:40:59Z")

</div>

On Aug 12, 4:16 pm, Sfdesigner S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
