Forum: Ruby Help on Directory Iteration

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Newb N. (Guest)
on 2009-01-27 08:34
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
Robert K. (Guest)
on 2009-01-27 08:55
(Received via mailing list)
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
Newb N. (Guest)
on 2009-01-27 09:02
Thanks for the timely reply
Peña, Botp (Guest)
on 2009-01-27 10:13
(Received via mailing list)
From: Newb N. [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 ;)
Newb N. (Guest)
on 2009-01-27 11:49
Newb N. wrote:
>
> Thanks for the timely reply

v
Newb N. (Guest)
on 2009-01-27 11:52
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
Brian C. (Guest)
on 2009-01-27 12:53
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
Robert K. (Guest)
on 2009-01-28 00:32
(Received via mailing list)
On 27.01.2009 10:47, Newb N. wrote:
> Newb N. wrote:
>> Thanks for the timely reply
>
> v

?
This topic is locked and can not be replied to.