Home > ruby > create folders & download files :: ruby

create folders & download files :: ruby

Again, another very small post. I’ve been tending to use ruby lately to perform little repetitive jobs. Currently I’m downloading files from one of our web servers. The requirement was to download multiple files and place them in their corresponding folders.


Creating folders using ruby is easy…use the file.makedirs command..examples below:

require 'ftools'
#to make directly under the directory
File.makedirs("d:\\foobar")
#Sub Folder..
File.makedirs("d:\\foobar\\blah")
#to make in directory where the script is saved
File.makedirs("foobar")

Enough said.

Now to download a file…

#grab a jpeg..
open('d:\\foobar\\mishmashmoo.jpg','wb').write(open("http://mishmashmoo.com/downloads/download.jpg").read)
#grab a zip file..
open('d:\\foobar\\mishmashmoo.zip','wb').write(open("http://mishmashmoo.com/downloads/webcam.zip").read)

Those examples will work…..the content is down loadable off my site, so give it a try.

Enjoy!

admin ruby

  1. May 26th, 2009 at 18:22 | #1

    Ruby rocks

  1. No trackbacks yet.