Skip to content
May 23, 2009 / Pratik

I tend to write a lot of little programs…

I tend to write a lot of little programs, because it’s more efficient to
write a script than doing the alternatives.

For example, it takes a long time to download a bunch of files and
organize those downloaded files. This program reads a table from a csv file.
Then it downloads files from the url’s in the second column and it puts the
downloaded file in the the folder in the first column. The lines starting
with an # (octothorpe) except the first line are water downed notes for
people who know don’t what the program is doing. The first line shows where
ruby is located.


# This program requires the csv library. A library is filled with
# functions, objects, classes (types of things) for the purpose of
# being re-used.
require 'csv'

# The parameters (or arguments) for the program is stored in an array (a
# list of things). This checks if the user put in the first parameter.
if ARGV[0] == nil
  # If the user didn't put in the first parameter then the script displays
  # a usage statement (how to use this program in the terminal). This
  # program exits after the usage statements is displayed.
  # $0 is the file name where the program is running from.
  puts $0 + " somefile.csv"

# I know this is not really necessary, but it waters things down. I know
# I can still use ARGV[0]. The first parameter should be the path to
# the CSV file. A path is the location of the file.
csv_file = ARGV[0]

# This checks if the file doesn't exist.
if !File.exist? csv_file
  # If the file doesn't exist then the program displays an error. The
  # program exits after the error (the file doesn't exist) is displayed.
  $stderr.puts csv_file + " does not exist."

# This program opens the file then reads one line at a time. Each line
# is a row in the table. CSV::Reader.parse is from the csv library.
CSV::Reader.parse( do |row|
  # The current row is stored as an array. For each row download from the
  # url in the second column and save it the folder in the the first
  # column. wget is an other program. wget is a powerful downloading
  # tool. wget is available for Windows, Mac OS X, Ubuntu and many other
  # operating systems.
  `wget --directory-prefix '#{row[0]}' #{row[1]}`

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: