# Search for string in folder names

**URL:** <https://www.ruby-forum.com/t/search-for-string-in-folder-names/158891>\
**Category:** Ruby\
**Created:** [January 29, 2009, 5:00am UTC](https://www.ruby-forum.com/t/search-for-string-in-folder-names/158891 "2009-01-29T05:00:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [January 29, 2009, 5:00am UTC](https://www.ruby-forum.com/t/search-for-string-in-folder-names/158891/1 "2009-01-29T05:00:44Z")

</div>

Hi …  
I will be getting some string through params[:first\_name].and i want to  
get that  
what are all the folders names contain that string.  
For example  
I have folders names like below  
each one is folder name

```
removed_email_address@domain.invalid~Vs~removed_email_address@domain.invalid
removed_email_address@domain.invalid~Vs~removed_email_address@domain.invalid
removed_email_address@domain.invalid~Vs~removed_email_address@domain.invalid

```

Now if i enter “joy” into that first name text box  
i want to get all the folders which has joy in the folder names  
for the above example i will have to get the below results for the  
folder search because it contains joy in the folder name

```
    removed_email_address@domain.invalid~Vs~removed_email_address@domain.invalid
    removed_email_address@domain.invalid~Vs~removed_email_address@domain.invalid

```

pls help me up how can i implemend this.  
Thanks in advance

Cheers

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [January 29, 2009, 5:06am UTC](https://www.ruby-forum.com/t/search-for-string-in-folder-names/158891/2 "2009-01-29T05:06:16Z")

</div>

On Thu, Jan 29, 2009 at 5:58 AM, Newb N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> removed\_email\_address@domain.invalid~Vs~removed\_email\_address@domain.invalid
> 
> pls help me up how can i implemend this.  
> Thanks in advance
> 
> ## Cheers
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Dir.glob(“/path/to/base/dir/_#{search\_string}_”)

Think carefully about the security of working with input directly from  
user  
input on the file system.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [January 29, 2009, 5:10am UTC](https://www.ruby-forum.com/t/search-for-string-in-folder-names/158891/3 "2009-01-29T05:10:21Z")

</div>

I am no expert in this, just my two cents,but I would do the following:

def getNames(param, directory = Dir.getwd)  
foldernames, matches = Dir.entries(directory), []  
foldernames.each { |fn|  
if fn.include?(param) then matches.push(fn) end  
}  
puts matches.join(“\n”)  
end

I’m not an expert as I’ve said, but for just creating a list of folders  
that  
match your parameters, such as joy, I think that would retrieve an array  
of  
strings for filenames that include the string passed for ‘param’ in the  
directory passed to ‘directory’ or the current directory if one is not  
specified…

Doesn’t solve your text box part, but I’d try something along the lines  
of  
what I put above to retrieve the filenames. Hope that helps you. 🙂

- Jayce

* * *

From: “Newb N.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, January 28, 2009 7:58 PM  
Newsgroups: comp.lang.ruby  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Search for string in folder names
