问题描述
||
我正在使用以下代码尝试使用Ruby从ftp获取所有文件。
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
如何从该字符串中提取文件名?
非常感谢
解决方法
您可以像这样使用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
, 如果要处理ftp.list的输出,则可能会发现net-ftp-list有用。
, 但是,ѭ3似乎很有用,因为您可以传递匹配的模式,但nlst
支持的模式似乎没有。我只是做了一个简单的技巧来使列表输出起作用:
ftp.list(\"*.zip\") do |zipfile|
zipfile = zipfile.split(/\\s+/).last
# ... do something with the file
end