有什么方法可以从 ruby​​ 中的 Net::SSH 或 NET::SFTP 命令修改日期吗?

发布于 2024-08-29 18:42:36 字数 230 浏览 4 评论 0原文

有没有一种简单的方法可以使用 Net::SFTP 获取文件的修改日期?

能够做到这一点就好了:

Net::SFTP.start('some_server') do |sftp|
  sftp.dir.glob('*').each do |file|
    puts file.mtime
  end
end

但据我所知,这是不可能的。

伯恩斯。

Is there an easy way to get the date modified of a file by using Net::SFTP?

It'd be nice to be able to do this:

Net::SFTP.start('some_server') do |sftp|
  sftp.dir.glob('*').each do |file|
    puts file.mtime
  end
end

But that's not possible (to my knowledge).

Berns.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-09-05 18:42:36

您的示例代码已经差不多了,您只需要在 file.mtime 处使用 file.attributes.mtime 即可。

另外,我猜测问题中的代码只是一个示例,但为了使其执行,您还需要将用户名和密码传递给 start 并传递路径和模式到glob。因此,一个有效的示例是:

Net::SFTP.start('some_server', 'mike', :password => 'secret') do |sftp|
  sftp.dir.glob('.', '*').each do |file|
    puts file.attributes.mtime
  end
end

mtime 返回的值将是自纪元以来的秒数,因此您可能需要将其传递给 Time.at 将其转换为 Time 对象。

如果您好奇,可以以相同方式使用的其他属性包括:

  • permissions
  • uid
  • gid
  • size
  • < code>atime(上次访问时间)

Your example code is almost there, you just need to use file.attributes.mtime where you had file.mtime.

Also, I'm guessing the code in the question was just an example, but in order for it to execute you also need to pass the username and password to start and pass the path as well as the pattern to glob. So a working example would be:

Net::SFTP.start('some_server', 'mike', :password => 'secret') do |sftp|
  sftp.dir.glob('.', '*').each do |file|
    puts file.attributes.mtime
  end
end

The value returned by mtime will be the number of seconds since the epoch so you may want to pass it to Time.at to convert it to a Time object.

In case you're curious, the other attributes available in the same way are:

  • permissions
  • uid
  • gid
  • size
  • atime (time of last access)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文