Download multiple FTP files like d*.txt in ruby
I need to connect to a ftp site and download开发者_如何学Go 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
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, ...
That solution did not work for me, although it may depend on the FTP server. For me, ftp.list returns results similar to ls -l
on Linux. I used the following regex to get just the filename of the first file returned by list:
ftp.list('D*.txt')[0][/.*(\d{2}):(\d{2})\s{1}(?<file>.+)$/,1]
精彩评论