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
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.
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.
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.
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, ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With