Like ruby itself, many libraries provide classes of exceptions
which can be handled by (a) rescue clause(s); usually within a
begin / end block. Below, only permanent errors are trapped and
the FTP#lastresp method is called to see what the trouble is.
Your result above shows that a ‘550’ response was received from
the server for ‘file/dir not found’ so, if it’s returned, a new
directory is created and the chdir is retried.
I just ran this on my 20MB of free virtual web-space that’s doing
nothing but being virtual at the moment with quite spectacular
success. The Ruby crew (or crubies as they’re never called who
write these libraries are absolute stars because, without them,
it wouldn’t just take longer for us to write a script …- more
than likely, it wouldn’t get started / finished at all.
Thanks in this particular case (Net::FTP) go to Shugo Maeda.
A few changes at the top for folks to make to personalise this.
#<–start–>
Personal FTP Log-In details - ((CHANGE))
MY_WEBSPACE_FTP = ‘homepag#s.##########’
ACCOUNT = nil # I’m a single user account (not used)
USER = ‘you’
PASSWORD = ‘opensesa-you’
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
— S E C U R I T Y —
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
Dir.chdir(‘C:\TEMP’) # local temp-file directory ???
TEMP_FILENAME = ‘test6.txt’
FTP_DIR_NAME = ‘rbjun14’
File to browse will be:
(Your-www-site)/rbjun14/test6.txt
If the correct details are entered above,
the rest of the script should work for most people.
Create a local text file to upload via FTP
line_t = format(“***** %s *****”, Time.now)
line_b = ‘’ << ‘=’ * (line_t.length-2) << '’
File.open(TEMP_FILENAME, ‘w’) do |tmp|
tmp.puts(line_b, line_t, line_b)
end
require ‘net/ftp’
ftp = Net::FTP.open(MY_WEBSPACE_FTP, USER, PASSWORD, ACCOUNT)
begin
retries = 1
begin
ftp.chdir(FTP_DIR_NAME)
rescue Net::FTPPermError => err
puts “Last response = #{ftp.lastresp}”
puts err
if ftp.lastresp == ‘550’ # No such directory
retries -= 1
unless retries < 0
### Create test directory ###
puts “Creating #{FTP_DIR_NAME} directory”
ftp.mkdir(FTP_DIR_NAME)
···
“John Jenniskens” John.Jenniskens@xs4all.nl wrote:
Hello,
I want to make a program that transfers a lot of files to another computer
by means of FTP. I want to check if the target directory exist. I want to do
a chdir, and if I retrieve a status indicating the directory doesn’t exist,
I want to issue a mkdir command.
When I issue a chdir (or even a list) on a non existing file, Ruby stops:
V:/RUBY/lib/ruby/1.6/net/ftp.rb:129:in getresp': 550 /abc: The system cannot find the file specified. (Net::FTPPermError) from V:/RUBY/lib/ruby/1.6/net/ftp.rb:137:in
voidresp’
from V:/RUBY/lib/ruby/1.6/net/ftp.rb:298:in retrlines' from V:/RUBY/lib/ruby/1.6/net/ftp.rb:285:in
mon_synchronize’
from V:/RUBY/lib/ruby/1.6/net/ftp.rb:285:in retrlines' from V:/RUBY/lib/ruby/1.6/net/ftp.rb:447:in
list’
How can I avoid this stopping, and getting the status in my program?
I’m using Ruby 1.6.6 on Windows2000.
Kind regards,
John Jenniskens
###
retry
end
end
raise 'Directory unavailable'
end
puts “Uploading file #{TEMP_FILENAME}”
ftp.puttextfile(TEMP_FILENAME, TEMP_FILENAME)
file_list = ftp.nlst(‘*’)
puts ‘-’ * 80
puts file_list
ftp.delete(TEMP_FILENAME)
ftp.chdir(‘…’) # parent dir
ftp.rmdir(FTP_DIR_NAME)
ensure
ftp.close ## Close Connection
File.delete(TEMP_FILENAME) # local (temp) text file
end
#<–end–>
#-> Last response = 550
#-> 550 No such directory.
#-> Creating rbjun14 directory
#-> Uploading file test6.txt
#-> --------------------------------------------------------------------------------
#-> test6.txt
daz