Reputation: 73
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
Upvotes: 6
Views: 4871
Reputation: 547
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, ...
Upvotes: 6
Reputation: 760
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]
Upvotes: 3
Reputation: 6840
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.
Upvotes: 7