Ruby Net :: FTP,从ftp.list()中提取文件名

Bre*_*ski 9 ruby ruby-on-rails

我正在使用以下代码尝试使用Ruby从ftp获取所有文件.

files = ftp.list()

files.each do |file|
  ftp.gettextfile(file)
end
Run Code Online (Sandbox Code Playgroud)

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

-rw-r--r-- 1 ftp ftp              0 May 31 11:18 brett.txt
Run Code Online (Sandbox Code Playgroud)

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

非常感谢

Jam*_*nan 18

您可以像这样使用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
Run Code Online (Sandbox Code Playgroud)


rub*_*olo -2

然而,这list似乎很有用,因为您可以传递一个匹配的模式,但它似乎不nlst支持。我刚刚做了一个快速而肮脏的黑客以使列表输出工作:

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