# How to use the Dir class, search for a sub directroy

**URL:** <https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711>\
**Category:** Ruby\
**Created:** [March 1, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711 "2009-03-01T15:14:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [March 1, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711/1 "2009-03-01T15:14:15Z")

</div>

Good day, I am learning Ruby,and doing some experaments.  
I have a bit of code that searches the header of MP3 files  
in a folder to get file 1 artist name. This is a variable artist.  
Now I try to search a diferent directory and see if  
a sub directory has the same name. So letts say the artist  
is Abba and I do have a sub folder in e:\music with ABBA  
how do I use Dir class to evaluate if Abba is found. I tried  
changing Abba to ABBA and still I do not get expected results.

Artist = ABBA  
Dir.chdir(“e:/music”)  
Dir["#{artist}"]

I tried a number of things here, I need a positive or negitave  
return; and, how to get the return value.

OH  
Is there a way to mark a post as answered?

---

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [March 1, 2009, 3:21pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711/2 "2009-03-01T15:21:49Z")

</div>

Harry N. schrieb:

> Dir.chdir(“e:/music”)  
> Dir[“#{artist}”]
> 
> I tried a number of things here, I need a positive or negitave  
> return; and, how to get the return value.
> 
> OH  
> Is there a way to mark a post as answered?  
> try this:  
> artist = ‘ABBA’  
> Dir.glob(“_#{artist}_”, File::FNM\_CASEFOLD)  
> the \* means anything before and after your search string, FNM\_CASEFOLD  
> =\> case insensitive  
> this could be easily found out by reading the rdoc at:  
> [class Dir - RDoc Documentation](http://www.ruby-doc.org/core/classes/Dir.html#M002347)  
> [class File - RDoc Documentation](http://www.ruby-doc.org/core/classes/File.html#M002603)

---

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [March 1, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711/3 "2009-03-01T16:06:01Z")

</div>

Thanks for your reply, I had read the doc but could not get it to work  
This is what I wound up doing and it does work.

Dir.chdir(“e:/foldertest”)  
artist = ‘Abba’  
artist\_check = Dir.glob(“#{artist}”, File::FNM\_CASEFOLD)  
puts artist\_check  
if “#{artist}” != “#{artist\_check}”  
puts “no match found”  
else puts “found match”  
end

badboy wrote:

> Harry N. schrieb:
> 
> > Dir.chdir(“e:/music”)  
> > Dir[“#{artist}”]
> > 
> > I tried a number of things here, I need a positive or negitave  
> > return; and, how to get the return value.
> > 
> > OH  
> > Is there a way to mark a post as answered?  
> > try this:  
> > artist = ‘ABBA’  
> > Dir.glob(“_#{artist}_”, File::FNM\_CASEFOLD)  
> > the \* means anything before and after your search string, FNM\_CASEFOLD  
> > =\> case insensitive  
> > this could be easily found out by reading the rdoc at:  
> > [class Dir - RDoc Documentation](http://www.ruby-doc.org/core/classes/Dir.html#M002347)  
> > [class File - RDoc Documentation](http://www.ruby-doc.org/core/classes/File.html#M002603)

---

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [March 1, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711/4 "2009-03-01T16:24:21Z")

</div>

I removed the \* as I only want exact match. There are only artist  
folders such as ABBA acdc they do not have additional folders.  
I do agree with you comment below; strange though, if I change  
the folder ABBA to FBBA my code returns no match found, then  
I change the name back to ABBA, I get found match. Sooo it does work,  
yet if it’s wrong it will then break some time. I think a different  
Dir class command is needed not glob, I am trying only for  
a ture or false return not the aray.

Again thanks for your input; and on Sunday too!

badboy wrote:

> Harry N. schrieb:
> 
> > end
> 
> Dir.glob returns an array, not a string, so your “if artist !=  
> artist\_check” will fail.  
> also, you won’t find folders/files like “abba album” with your search,  
> if you want this add the star ("\*")  
> artist is already a string, so no need for “#{…}”, just use artist  
> and at last:  
> if you changed the if-construct it will fail, if Dir.glob found “abba”,  
> because your “artist” is “Abba” 😉

---

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [March 1, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711/5 "2009-03-01T16:28:45Z")

</div>

On Sun, Mar 1, 2009 at 7:45 PM, Harry N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Artist = ABBA  
> Dir.chdir(“e:/music”)  
> Dir[“#{artist}”]
> 
> I tried a number of things here, I need a positive or negitave  
> return; and, how to get the return value.

If all you need is to know whether the directory exists, then  
File.exist?(‘ABBA’)  
will suffice (although it is case-sensitive).

Solidarity,  
lasitha

---

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [March 1, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711/6 "2009-03-01T16:15:47Z")

</div>

Harry N. schrieb:

> end

Dir.glob returns an array, not a string, so your “if artist !=  
artist\_check” will fail.  
also, you won’t find folders/files like “abba album” with your search,  
if you want this add the star ("\*")  
artist is already a string, so no need for “#{…}”, just use artist  
and at last:  
if you changed the if-construct it will fail, if Dir.glob found “abba”,  
because your “artist” is “Abba” 😉

---

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [March 1, 2009, 4:45pm UTC](https://www.ruby-forum.com/t/how-to-use-the-dir-class-search-for-a-sub-directroy/161711/7 "2009-03-01T16:45:31Z")

</div>

Yes this too works, it is simple too.  
Thanks four your help!

lasitha wrote:

> On Sun, Mar 1, 2009 at 7:45 PM, Harry N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Artist = ABBA  
> > Dir.chdir(“e:/music”)  
> > Dir[“#{artist}”]
> > 
> > I tried a number of things here, I need a positive or negitave  
> > return; and, how to get the return value.
> 
> If all you need is to know whether the directory exists, then  
> File.exist?(‘ABBA’)  
> will suffice (although it is case-sensitive).
> 
> Solidarity,  
> lasitha
