Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download multiple FTP files like d*.txt in ruby

Tags:

ruby

I need to connect to a ftp site and download a bunch of files( max 6) named as D*.txt. could you please help me code this in Ruby ? The following code just

ftp = Net::FTP::new("ftp_server_site")
ftp.login("user", "pwd")
ftp.chdir("/RemoteDir")
fileList= ftp.nlst
ftp.getbinaryfile(edi, edi)
ftp.close

Thanks

like image 887
sada Avatar asked Nov 23 '10 10:11

sada


People also ask

How do I download all files from an FTP folder?

To download multiple files or folders, click/tap+drag or CTRL/CMD+click the files/folders, then click the icon in the footer toolbar. The files/folders will be zipped on the server and your browser will prompt you to open or save the file to your device.

How do you download a file in Ruby?

Plain old Ruby The most popular way to download a file without any dependencies is to use the standard library open-uri . open-uri extends Kernel#open so that it can open URIs as if they were files. We can use this to download an image and then save it as a file.


2 Answers

The simplest way would be to loop through the list of files in fileList.

Here is an example (untested):

ftp = Net::FTP::new("ftp_server_site")
ftp.login("user", "pwd")
ftp.chdir("/RemoteDir")
fileList = ftp.list('D*.txt')
fileList.each do |file|
  ftp.gettextfile(file)
end
ftp.close

Hope this helps.

like image 182
Brian Avatar answered Sep 22 '22 14:09

Brian


Array of filenames in dir you can get by "nlst" method:

files = ftp.nlst('*.zip')

files.each do |file|
  puts file
end

#=> first.zip, second.zip, third.zip, ...
like image 31
Stefan Huska Avatar answered Sep 25 '22 14:09

Stefan Huska