Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Help on Directory Iteration

Reply
Thread Tools

Help on Directory Iteration

 
 
Newb Newb
Guest
Posts: n/a
 
      01-27-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-27-2009
On 27.01.2009 07:32, Newb Newb wrote:
> 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|


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

--
remember.guy do |as, often| as.you_can - without end
 
Reply With Quote
 
 
 
 
Newb Newb
Guest
Posts: n/a
 
      01-27-2009

Thanks for the timely reply
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Peña, Botp
Guest
Posts: n/a
 
      01-27-2009
RnJvbTogTmV3YiBOZXdiIFttYWlsdG86cmV2YXRoeS5wQGFuZ2 xlcml0ZWNoLmNvbV0gDQojICAg
RGlyLmZvcmVhY2goIkNoYXRIaXN0b3J5IikgZG8gfGZ8DQojIC AgICBpZiBmID09IHBhcmFtc1s6
ZV9kYXRlXVswXQ0KIyAgICAgICAgcHV0cyAidGhlcmU6Ig0KIy AgICAgICAgRGlyLmZvcmVhY2go
ZikgZG8gfHB8DQojICAgICAgICBwdXRzIHANCiMgICAgICAgIG VuZA0KIyAgICAgZW5kDQojICAg
ZW5kDQoNCmhtbSwgZm9sZGVyL2ZpbG5hbWVzIGFyZSB1bmlxIG ZvciBlYWNoIGxldmVsLCBzbyB5
b3Ugc2hvdWxkICpub3QgbmVlZCB0byBpdGVyYXRlIHRoYXQgbm 8/IHRoYXQgaXMsDQoNCiAgRGly
LmZvcmVhY2goJ0NoYXRIaXN0b3J5LycrcGFyYW1zWzplX2RhdG VdWzBdKSBkbyB8cGF0aHwNCiAg
ICBwdXRzIHBhdGggdW5sZXNzICV3KC4gLi4pLmluY2x1ZGU/IHBhdGgNCiAgZW5kDQoNCmFuZCBp
IHdvdWxkIGFsc28gcmVmcmFpbiB1c2luZyBwIGFzIGEgdmFyIG 5hbWUgOykNCg0K

 
Reply With Quote
 
Newb Newb
Guest
Posts: n/a
 
      01-27-2009
Newb Newb wrote:
>
> Thanks for the timely reply


v
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Newb Newb
Guest
Posts: n/a
 
      01-27-2009
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 =>
(E-Mail Removed)~Vs~kann...leritech .com
second is => testing
14;23;(E-Mail Removed)
in this i don't need the folder name which has date in
that.
that i don't need testing
14;23;(E-Mail Removed)
but i need
(E-Mail Removed)~Vs~kann...leritech .com
how can i do that help me up pls
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      01-27-2009
Newb Newb 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-27-2009
On 27.01.2009 10:47, Newb Newb wrote:
> Newb Newb wrote:
>> Thanks for the timely reply

>
> v


?

--
remember.guy do |as, often| as.you_can - without end
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Iteration Chris McComas Python 6 10-19-2008 02:54 AM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Showing value of loop iteration in assert statement dwerdna VHDL 5 03-31-2005 05:23 PM
looping and displaying progress with every iteration Bruce Whitehouse ASP .Net 3 11-23-2003 07:15 PM



Advertisments