Forum: Ruby list folder content from win directory -> xml output

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.
99bda7d3c145a348b5b690fb4dc135e8?d=identicon&s=25 First Last (memetical)
on 2006-05-13 18:19
hi everybody,

I've been searching all over the place but found nothing helpful yet.
I need to list a directory with all its subdirectories and files and
output that information into an xml file.
I think Ruby might be quite helpful on this task.
Since I am a newbie to Ruby I really don't know where to start.
Maybe somebody has a solution to this?

Thanx in advance!

memetical
Ced5fff44ff8929fc974012ea108b284?d=identicon&s=25 Sergey Volkov (rf-vsv)
on 2006-05-13 18:36
(Received via mailing list)
just to start:
#
puts "<all_files>"
Dir["**/*"].each{ |f| puts "  <file>#{f}</file>" }
puts "</all_files>"
#
this code prints all files in all subdirectories starting from current;
good luck
Sergey

----- Original Message -----
From: "jonas tracht" <jonas.tracht@gmail.com>
Sent: Saturday, May 13, 2006 12:19 PM
Subject: list folder content from win directory -> xml output
99bda7d3c145a348b5b690fb4dc135e8?d=identicon&s=25 First Last (memetical)
on 2006-05-13 19:52
Thanx Sergey,

so how do I transfer the lot into an xml file?
anybody, have a hint? maybe somebody can point me to valuable resources.
any help is higly appreciated!
thx,
jonas
Ced5fff44ff8929fc974012ea108b284?d=identicon&s=25 Sergey Volkov (rf-vsv)
on 2006-05-13 20:28
(Received via mailing list)
just to continue after previous start :) :
#
open( "all_files.xml", "w" ){ |out|
    out.puts "<all_files>"
    Dir["**/*"].each{ |f| out.puts "  <file>#{f}</file>" }
    out.puts "</all_files>"
}
#
this code outputs list of all files in output file in xml format:
'all_files.xml';

resources: you can start with
http://www.rubycentral.com/book/
http://www.ruby-lang.org/en/

enjoy
Sergey

----- Original Message -----
From: "jonas tr" <jonas.tracht@gmail.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Saturday, May 13, 2006 1:52 PM
Subject: Re: list folder content from win directory -> xml output
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-05-13 20:34
(Received via mailing list)
This task is quite simple.

You can solve this yourself without any real problems.

Look at class Dir

Mikkel

On Sunday, May 14, 2006, at 1:19 AM, jonas tracht wrote:
>
>memetical
>
>--
>Posted via http://www.ruby-forum.com/.
>


Mikkel Bruun

www.strongside.dk    - Football Portal(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
99bda7d3c145a348b5b690fb4dc135e8?d=identicon&s=25 First Last (memetical)
on 2006-05-13 20:36
thx a lot everybody.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-05-13 20:43
(Received via mailing list)
Sergey Volkov wrote:
> just to continue after previous start :) :
> #
> open( "all_files.xml", "w" ){ |out|
>    out.puts "<all_files>"
>    Dir["**/*"].each{ |f| out.puts "  <file>#{f}</file>" }
>    out.puts "</all_files>"
> }
> #
> this code outputs list of all files in output file in xml format:
> 'all_files.xml';

You may need to replace special characters

class String
   # For demo purposes only.
   def xscape
     self.gsub( '&', '&amp;').gsub( '<', '&lt; )
   end
end


And then call

     Dir["**/*"].each{ |f| out.puts "  <file>#{f.xscape}</file>" }


For extra fun consider adding file attributes as element attributes:

     Dir["**/*"].each{ |f|
       fl = File.new(f)
       mtime = " mtime='#{fl.mtime.to_s.xscape}' "
       ctime = " ctime='#{fl.ctime.to_s.xscape}' "
       out.puts "  <file #{mtime} #{ctime} >#{f.xscape}</file>"
      }

More or less.
>
> resources: you can start with
> http://www.rubycentral.com/book/
> http://www.ruby-lang.org/en/

and

   http://www.ruby-doc.org

There may be some useful stuff at

   http://www.rubyxml.com

as well.


--
James Britt

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
This topic is locked and can not be replied to.