# Regular expression and reading directories

**URL:** <https://www.ruby-forum.com/t/regular-expression-and-reading-directories/84070>\
**Category:** Rails\
**Created:** [February 1, 2007, 3:42am UTC](https://www.ruby-forum.com/t/regular-expression-and-reading-directories/84070 "2007-02-01T03:42:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [February 1, 2007, 3:42am UTC](https://www.ruby-forum.com/t/regular-expression-and-reading-directories/84070/1 "2007-02-01T03:42:35Z")

</div>

I have to read in a list of folders and display them to the user.  
However, there are certain folders that I don’t want to show up, and I’m  
trying to restrict this using regular expressions, which I’m not so good  
at.

There are things like “.” and “…” and “\_svn” that show up, that I’m  
trying to restrict.

Here’s my method (which isn’t working):

def view\_directory  
@files\_and\_folders = []  
reg\_exp = Regexp.new(/^$/) # what regexp goes in here?  
Dir.foreach("#{RAILS\_ROOT}/public/folder") do |f|  
@files\_and\_folders \<\< f if f != reg\_exp  
end  
end

This may not even be the best way to handle this, as I’ve never dealt  
with files/directories before. Any thoughts on the regular expression,  
or a better way? Thanks.

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [February 1, 2007, 5:40am UTC](https://www.ruby-forum.com/t/regular-expression-and-reading-directories/84070/2 "2007-02-01T05:40:38Z")

</div>

You could try:

directory = Dir.open("#{RAILS\_ROOT}/public/folder")  
@files\_and\_folders = directory.to\_a.select {|i| /(^…?$)|(.svn  
$)/.match(i) }

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [February 1, 2007, 6:42am UTC](https://www.ruby-forum.com/t/regular-expression-and-reading-directories/84070/3 "2007-02-01T06:42:40Z")

</div>

On 2/1/07, rph [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have to read in a list of folders and display them to the user.  
> However, there are certain folders that I don’t want to show up, and I’m  
> trying to restrict this using regular expressions, which I’m not so good  
> at.
> 
> There are things like “.” and “…” and “\_svn” that show up, that I’m  
> trying to restrict.

Might this do?:

Dir[“#{RAILS\_ROOT}/public/folder/\*”]

Dir.[] (and Dir.glob) are much like a unix shell glob in that it will  
omit dotfiles unless the glob starts with a dot.

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [February 1, 2007, 2:24pm UTC](https://www.ruby-forum.com/t/regular-expression-and-reading-directories/84070/4 "2007-02-01T14:24:49Z")

</div>

jdswift wrote:

> You could try:
> 
> directory = Dir.open("#{RAILS\_ROOT}/public/folder")  
> @files\_and\_folders = directory.to\_a.select {|i| /(^…?$)|(.svn  
> $)/.match(i) }

I think this solution will work just fine, but it actually returns  
exactly what I don’t want. How can I say to return the opposite?  
Essentially…

@files\_and\_folders = !directory…

Thanks!

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [February 1, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/regular-expression-and-reading-directories/84070/5 "2007-02-01T14:50:13Z")

</div>

rph wrote:

> jdswift wrote:
> 
> > You could try:
> > 
> > directory = Dir.open("#{RAILS\_ROOT}/public/folder")  
> > @files\_and\_folders = directory.to\_a.select {|i| /(^…?$)|(.svn  
> > $)/.match(i) }
> 
> I think this solution will work just fine, but it actually returns  
> exactly what I don’t want. How can I say to return the opposite?  
> Essentially…
> 
> @files\_and\_folders = !directory…
> 
> Thanks!

Nevermind… I just used “reject” instead of “select”. I forgot about  
that… thanks!
