2011-05-31 44 views
7

我正在使用下面的代码尝试使用Ruby从ftp获取所有文件。Ruby Net :: FTP,从ftp.list()提取文件名

files = ftp.list() 

files.each do |file| 
    ftp.gettextfile(file) 
end 

问题是,ftp.list返回一整行信息,而不仅仅是文件名,例如,

-rw-r--r-- 1 ftp ftp    0 May 31 11:18 brett.txt 

如何从该字符串中提取filname?

非常感谢

+0

解决。 啊我应该使用ftp.nlist()来获取文件名。 – Brettski 2011-05-31 01:43:04

+1

ftp.nlst,而不是ftp.nlist。 – 2012-09-18 17:16:48

回答

-2

然而,list似乎是有用的,因为你可以在匹配模式通过,它不会出现nlst支持。我只是做了一个快速和肮脏的黑客攻击,使列表输出工作:

ftp.list("*.zip") do |zipfile| 
    zipfile = zipfile.split(/\s+/).last 
    # ... do something with the file 
end 
+0

'nlst'支持模式以及... – severin 2013-12-04 10:40:57

0

如果你要处理的ftp.list你会发现net-ftp-list有用的输出。

16

可以使用nlst公共方法这样

files = ftp.nlst("*.zip")|ftp.nlst("*.txt")|ftp.nlst("*.xml") 

#optionally exclude falsely matched files 
exclude = /\.old|temp/ 

#exclude files with 'old' or 'temp' in the name 
files = files.reject{ |e| exclude.match e } #remove files matching the exclude regex 

files.each do |file| 
    #do something with each file here 
end