Skip to content

Instantly share code, notes, and snippets.

@maricris-sn
Created February 14, 2010 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maricris-sn/303957 to your computer and use it in GitHub Desktop.
Save maricris-sn/303957 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/sftp'
require 'faster_csv'
# Load our constants
host = "my.server.com"
user = "my_username"
password = "my_password"
port = 12345
dirs_to_create = "dirs_to_create.txt"
your_csv = "path_to_csv.csv"
csv_column_separator = ";"
path_to_error_log = "errors.txt"
# Get all directories to be made
dirs = File.readlines(dirs_to_create).map { |line| line.chomp }
# Attempt to create them all
unless dirs.empty?
dirs.each do |mydir|
FileUtils.mkdir_p mydir
puts "Created #{mydir} folder"
end
end
# Now, execute the downloads
FasterCSV.foreach(your_csv, :col_sep => csv_column_separator, :row_sep => :auto) do |row|
File.open(path_to_error_log, 'w') do |error_log|
Net::SFTP.start(host, user, :password => password, :port => port) do |sftp|
puts "Dowloading #{row[0]} to #{row[1]}..."
begin
download = sftp.download(row[0], row[1])
download.wait
rescue Exception => ex
error_log.puts "Encountered error: #{ex}"
end
end
end
end
require 'rubygems'
require 'net/sftp'
# Load our constants
host = "my.server.com"
user = "my_username"
password = "my_password"
port = 12345
dirs_to_create = "dirs_to_create.txt"
sftp_from_paths = "sftp_from_paths.csv"
local_to_paths = "local_to_paths.csv"
path_to_error_log = "errors.txt"
# Get all directories to be made
dirs = File.readlines(dirs_to_create).map { |line| line.chomp }
froms = File.readlines(sftp_from_paths).map { |line| line.chomp }
tos = File.readlines(local_to_paths).map { |line| line.chomp }
# Attempt to create them all
unless dirs.empty?
dirs.each do |mydir|
FileUtils.mkdir_p mydir
puts "Created #{mydir} folder"
end
end
# Now, execute the downloads
if !(froms.empty? or tos.empty?) and (froms.size.eql?(tos.size))
File.open(path_to_error_log, 'w') do |error_log|
Net::SFTP.start(host, user, :password => password, :port => port) do |sftp|
froms.each_with_index do |from_path, index|
puts "Dowloading #{from_path} to #{tos[index]}..."
begin
download = sftp.download(from_path, tos[index])
download.wait
rescue Exception => ex
error_log.puts "Encountered error: #{ex}"
end
end
end
end
else
puts "Your remote paths and local paths are not equal, or one of them is empty."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment