List folder content from win directory -> xml output


#1

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


#2

just to start:

puts “<all_files>”
Dir["**/*"].each{ |f| puts " #{f}" }
puts “</all_files>”

this code prints all files in all subdirectories starting from current;
good luck
Sergey

----- Original Message -----
From: “jonas tracht” removed_email_address@domain.invalid
Sent: Saturday, May 13, 2006 12:19 PM
Subject: list folder content from win directory -> xml output


#3

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


#4

just to continue after previous start :slight_smile: :

open( “all_files.xml”, “w” ){ |out|
out.puts “<all_files>”
Dir["**/*"].each{ |f| out.puts " #{f}" }
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” removed_email_address@domain.invalid
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” removed_email_address@domain.invalid
Sent: Saturday, May 13, 2006 1:52 PM
Subject: Re: list folder content from win directory -> xml output


#5

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 B.

www.strongside.dk - Football Portal(DK)
ting.minline.dk - Buy Old Stuff!(DK)


#6

Sergey V. wrote:

just to continue after previous start :slight_smile: :

open( “all_files.xml”, “w” ){ |out|
out.puts “<all_files>”
Dir["**/*"].each{ |f| out.puts " #{f}" }
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( ‘&’, ‘&’).gsub( ‘<’, '< )
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 B.

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


#7

thx a lot everybody.