andy
6/25/2007 8:07:00 PM
Manish Sapariya schrieb:
> Hi,
> I am trying to copy a remote directroy recursively using Net::sftp.
>
> I could not find any direct api given by this module and decided to
> copy the entire directory file by file.
>
> However i am not able to figure out, how to tell between file or directory.
>
> I am trying to follow simple method,
>
> for every file in directory
> if file is direcory
> create directory
> else
> get/put directory
> end
>
> However I am not able to find out any method to figure out the type of
> file on remote machine.
>
> Any ideas?
> Thanks and Regards,
> Manish
>
>
>
>
I just had this same problem, while I didn't solve your precise issue;
the following code will recursively download the contents of a folder.
It's rather crude, if anyone has a refactor suggestion I'm quite open to
hear it.
#################################################
require 'rubygems'
require 'net/sftp'
def open_or_get_all(sftp, open_dir, local_dir)
handle = sftp.opendir(open_dir)
items = sftp.readdir(handle)
items.each do |item|
if item.filename != '.' && item.filename != '..'
if item.longname[0...1] == 'd'
# mkdir locally
Dir.mkdir(local_dir + item.filename, 0777)
# open dir and download all
open_or_get_all(sftp, open_dir + item.filename + '/', local_dir
+ item.filename + '/')
else
#puts local_dir+item.filename
#puts open_dir+item.filename
sftp.get_file open_dir+item.filename, local_dir+item.filename
end
end
end
sftp.close_handle(handle)
end
Net::SFTP.start('ext_server', 'user_name', 'password') do |sftp|
open_or_get_all(sftp, "/home/user_name/dir_to_dnld/", "/lcl_dir/")
end
#################################################
maybe that will save someone else a few minutes...