Jamey Cribbs
9/12/2006 3:14:00 PM
Lincoln Anderson wrote:
> Here's where my problem comes in. How can I tell from the list array
> whether an item is a directory or a file to be copied? Is there
> something I can use (such as an FTP command) to catch a clue on how to
> implement this? I know this is possible, as ftpmirror is done with
> PERL and Ruby can do anything PERL can do - better :)
>
Here's a class I defined to use in a script I run that moves files
between various Unix and Windows servers at my company. Haven't looked
at the code in a while, so it might be ugly, but it works!
class FTPLocation
def initialize(host, userid, password, path)
@host, @userid, @password = host, userid, password
if path.nil?
@path = './'
else
@path = path
end
end
#-----------------------------------------------------------------------
# to_s
#-----------------------------------------------------------------------
def to_s
return '%s - %s' % [@host, @path]
end
#-----------------------------------------------------------------------
# connect
#-----------------------------------------------------------------------
def connect
@ftp = Net::FTP.new(@host, @userid, @password)
# @ftp.debug_mode = true
@ftp.chdir(@path)
end
#-----------------------------------------------------------------------
# empty?
#-----------------------------------------------------------------------
def empty?
@ftp.list.each do |entry|
next if entry[0] == 'd'
return false
end
return true
end
#-----------------------------------------------------------------------
# each_file
#-----------------------------------------------------------------------
def each_file
t = Time.now
@ftp.list.each do |entry|
next if entry[0] == 'd'
filename = entry.split(' ')[-1]
next if t - @ftp.mtime(filename) < 300
yield filename
end
end
#-----------------------------------------------------------------------
# file_exists?
#-----------------------------------------------------------------------
def file_exists?(filename)
@ftp.list.each do |entry|
next if entry[0] == 'd'
return true if filename == entry.split(' ')[-1]
end
return false
end
#-----------------------------------------------------------------------
# get_file
#-----------------------------------------------------------------------
def get_file(filename, dest_path, xfer_type)
if xfer_type == 'binary'
@ftp.getbinaryfile(filename, dest_path)
else
@ftp.gettextfile(filename, dest_path)
end
end
#-----------------------------------------------------------------------
# put_file
#-----------------------------------------------------------------------
def put_file(source_path, filename, xfer_type)
if xfer_type == 'binary'
@ftp.putbinaryfile(source_path, filename)
else
@ftp.puttextfile(source_path, filename)
end
end
#-----------------------------------------------------------------------
# delete_file
#-----------------------------------------------------------------------
def delete_file(filename)
@ftp.delete(filename)
end
#-----------------------------------------------------------------------
# close
#-----------------------------------------------------------------------
def close
@ftp.close if @ftp
end
end
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.