CURL moving files on FTP

Hi Guys,

I would like to download a file on an ftp site using curl and then move then file on the remote site to another folder i.e. Downloaded so that when I list the directory again I don’t see the downloaded files. Can anyone point in the right direction please? Here’s what I have already:

set fileToDownload to "LMB3006.pdf"

set filesList to every paragraph of (do shell script "curl -l -u username:password [url=ftp://www.theAddress.com/site783/)]ftp://www.theAddress.com/site783/")[/url]

if fileToDownload is in filesList then
	do shell script "cd ~/Desktop; curl -O ftp://username:password@wwwtheAddress.com/site783/" & fileToDownload
end if

Many Thanks,
Nik

This is pretty easy to do with Ruby.

require ‘net/ftp’

username = ‘username’
password = ‘password’
host = ‘theAddress.com
download_directory = ‘site783’
destination_directory = ‘change-me’

begin
ftp = Net::FTP::new(host, username, password)
ftp.chdir(download_directory)
ftp.nlst.each do |file|
next if file =~ /^./ # skip dot files
ftp.getbinaryfile(file)
ftp.delete(file)
ftp.chdir(destination_directory)
ftp.putbinaryfile(file)
end
rescue Net::FTPTempError
puts “No files to download”
ensure
ftp.close
end

Hi Craig,

I must admit that I’ve not used Ruby before but always open to new ideas!

I saved your script as a .rb file in TextWrangler and then ran it like this:

set rubyScript to "ruby /Users/me/Documents/Ruby/filetest.rb | bin/bash"

do shell script rubyScript

How would I pass it the name of the file that I want to move?

Thanks,
Nik

Arguments are held inside an array and you can access them using ARGV.

require ‘net/ftp’

if ARGV.size != 1
puts “You must provide the filename you wish to download”
exit
end

filename = ARGV[0]

username = ‘username’
password = ‘password’
host = ‘theAddress.com
download_directory = ‘site783’
destination_directory = ‘change-me’

begin
ftp = Net::FTP::new(host, username, password)
ftp.chdir(download_directory)
files = ftp.nlst

if files.include?(filename)
ftp.getbinaryfile(filename)
ftp.delete(filename)
ftp.chdir(destination_directory)
ftp.putbinaryfile(filename)
end
rescue Net::FTPTempError
puts “No files to download”
ensure
ftp.close
end

And call it like so.

set rubyScript to "ruby /Users/me/Documents/Ruby/filetest.rb" & space & "filename.png"