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站点并下载一堆名为D * .txt的文件(最多6个)。你能帮我用Ruby编写代码吗?以下代码只是
ftp = Net::FTP::new("ftp_server_site")
ftp.login("user", "pwd")
ftp.chdir("/RemoteDir")
fileList= ftp.nlst
ftp.getbinaryfile(edi, edi)
ftp.close
Thanks
3 个解决方案
#1
7
The simplest way would be to loop through the list of files in fileList
.
最简单的方法是循环遍历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.
希望这可以帮助。
#2
6
Array of filenames in dir you can get by "nlst" method:
dir中的文件名数组,您可以通过“nlst”方法获取:
files = ftp.nlst('*.zip')
files.each do |file|
puts file
end
#=> first.zip, second.zip, third.zip, ...
#3
3
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服务器。对我来说,ftp.list在Linux上返回类似于ls -l的结果。我使用以下正则表达式来获取list返回的第一个文件的文件名:
ftp.list('D*.txt')[0][/.*(\d{2}):(\d{2})\s{1}(?<file>.+)$/,1]
#1
7
The simplest way would be to loop through the list of files in fileList
.
最简单的方法是循环遍历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.
希望这可以帮助。
#2
6
Array of filenames in dir you can get by "nlst" method:
dir中的文件名数组,您可以通过“nlst”方法获取:
files = ftp.nlst('*.zip')
files.each do |file|
puts file
end
#=> first.zip, second.zip, third.zip, ...
#3
3
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服务器。对我来说,ftp.list在Linux上返回类似于ls -l的结果。我使用以下正则表达式来获取list返回的第一个文件的文件名:
ftp.list('D*.txt')[0][/.*(\d{2}):(\d{2})\s{1}(?<file>.+)$/,1]