# Help on Directory Iteration

**URL:** <https://www.ruby-forum.com/t/help-on-directory-iteration/158715>\
**Category:** Ruby\
**Created:** [January 27, 2009, 7:34am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715 "2009-01-27T07:34:55Z")\
**Posts on this page:** 8\
**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 27, 2009, 7:34am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/1 "2009-01-27T07:34:55Z")

</div>

Hi People i have never done file or Directory manipulations before.  
now in my form i have given date field and search button.  
if the user enters date and clicks the search button,accoding to the  
date entered it has to search for the folder in the ChatHistory folder  
for example user enters date like 2009-01-23 means it has to search for  
the folder which named 2009-01-23.  
if the foder exists again it has to be iterated when i do this i get  
error.  
folder structure would be ChatHistory has one folder like 2009-01-23  
has two folder namely test and user these two folders contain 2 files  
each.  
My code starts here  
if params[:first\_name].blank? and params[:second\_name].blank? and  
!params[:e\_date][0].blank? and params[:e\_date][1].blank?  
puts “frtst date is not blank others are blank”  
Dir.foreach(“ChatHistory”) do |f|  
if f == params[:e\_date][0]  
puts “there:”  
Dir.foreach(f) do |p|  
puts p  
end  
end  
end

Pls Kindly help me up

---

<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 27, 2009, 7:55am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/2 "2009-01-27T07:55:55Z")

</div>

On 27.01.2009 07:32, Newb N. wrote:

> each.  
> My code starts here  
> if params[:first\_name].blank? and params[:second\_name].blank? and  
> !params[:e\_date][0].blank? and params[:e\_date][1].blank?  
> puts “frtst date is not blank others are blank”  
> Dir.foreach(“ChatHistory”) do |f|  
> if f == params[:e\_date][0]  
> puts “there:”  
> Dir.foreach(f) do |p|

f does not contain the full path here so you need something like:

```
    Dir.foreach(File.join("ChatHistory",f)) do |p|

```

Btw, it’s often helpful to put a few printing statements here and there  
during debugging. 🙂

> ```
> puts p
> end
> end
> 
> ```
> 
> end

Kind regards

robert

---

<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 27, 2009, 8:02am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/3 "2009-01-27T08:02:59Z")

</div>

Thanks for the timely reply

---

<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 27, 2009, 9:13am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/4 "2009-01-27T09:13:53Z")

</div>

From: Newb N. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Dir.foreach(“ChatHistory”) do |f|

# if f == params[:e\_date][0]

# puts “there:”

# Dir.foreach(f) do |p|

# puts p

# end

# end

# end

hmm, folder/filnames are uniq for each level, so you should \*not need to  
iterate that no? that is,

Dir.foreach(‘ChatHistory/’+params[:e\_date][0]) do |path|  
puts path unless %w(. …).include? path  
end

and i would also refrain using p as a var name 😉

---

<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 27, 2009, 10:49am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/5 "2009-01-27T10:49:36Z")

</div>

Newb N. wrote:

> Thanks for the timely reply

v

---

<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 27, 2009, 11:53am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/6 "2009-01-27T11:53:59Z")

</div>

Newb N. wrote:

> folder structure would be ChatHistory has one folder like 2009-01-23  
> has two folder namely test and user these two folders contain 2 files  
> each.

You can get a list of all files and subdirectories under a subdirectory  
like this:

Dir[“ChatHistory/2009-01-23/\*\*/\*”]

Note: if you are inserting a value from a parameter, you should sanitise  
it first. At least remove /…/, although it’s safest to allow only valid  
values like this:

date = params[:date]  
raise “Bad date format” unless date =~ /\A\d\d\d\d-\d\d-\d\d\z/  
Dir[“ChatHistory/#{date}/\*\*/\*”].each do |f|  
…  
end

---

<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 27, 2009, 10:52am UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/7 "2009-01-27T10:52:08Z")

</div>

if params[:first\_name].blank? and params[:second\_name].blank? and  
!params[:e\_date][0].blank? and params[:e\_date][1].blank?  
puts “frtst date is not blank others are blank”  
Dir.foreach(“ChatHistory”) do |folder\_name|  
if folder\_name == params[:e\_date][0]  
puts “there:”  
puts folder\_name  
@sub\_folder\_one = Array.new  
Dir.foreach(File.join(“ChatHistory”,folder\_name)) do  
|sub\_folder\_name|  
@sub\_folder\_one \<\< sub\_folder\_name  
end  
end  
end  
end  
@sub\_folder\_one array has two folder names  
one is =\>  
removed\_email\_address@domain.invalid~Vs~removed\_email\_address@domain.invalid  
second is =\> testing  
14;23;removed\_email\_address@domain.invalid  
in this i don’t need the folder name which has date in  
that.  
that i don’t need testing  
14;23;removed\_email\_address@domain.invalid  
but i need  
removed\_email\_address@domain.invalid~Vs~removed\_email\_address@domain.invalid  
how can i do that help me up pls

---

<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 27, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/help-on-directory-iteration/158715/8 "2009-01-27T23:32:17Z")

</div>

On 27.01.2009 10:47, Newb N. wrote:

> Newb N. wrote:
> 
> > Thanks for the timely reply
> 
> v

?
