Ruby FTP从文件夹中分离文件 [英] Ruby FTP Separating files from Folders

查看:147
本文介绍了Ruby FTP从文件夹中分离文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图抓取FTP并递归地下拉所有文件。

I'm trying to crawl FTP and pull down all the files recursively.

到目前为止,我试图用

Up until now I was trying to pull down a directory with

   ftp.list.each do |entry|
    if entry.split(/\s+/)[0][0, 1] == "d"
      out[:dirs] << entry.split.last unless black_dirs.include? entry.split.last
    else
      out[:files] << entry.split.last unless black_files.include? entry.split.last
    end

但事实证明,如果您将列表分割为最后一个空格,文件名和带空格的目录是错误的。
在这里需要一些帮助。

But turns out, if you split the list up until last space, filenames and directories with spaces are fetched wrong. Need a little help on the logic here.

推荐答案

您也可以使用正则表达式。我把一个放在一起。请验证它是否适用于您,以及我不知道您的目录列表看起来不同。您必须使用Ruby 1.9 btw。

You can also use a regular expression. I put one together. Please verify if it works for you as well as I don't know it your dir listing look different. You have to use Ruby 1.9 btw.

reg = /^(?<type>.{1})(?<mode>\S+)\s+(?<number>\d+)\s+(?<owner>\S+)\s+(?<group>\S+)\s+(?<size>\d+)\s+(?<mod_time>.{12})\s+(?<path>.+)$/

match = entry.match(reg)

您可以通过名称访问元素,然后

You are able to access the elements by name then

match [:type] 包含一个'd'如果它是一个文件。

match[:type] contains a 'd' if it's a directory, a space if it's a file.

所有其他元素也在那里。最重要的是 match [:path]

All the other elements are there as well. Most importantly match[:path].

这篇关于Ruby FTP从文件夹中分离文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆