有什么方法可以从 ruby 中的 Net::SSH 或 NET::SFTP 命令修改日期吗?
有没有一种简单的方法可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例代码已经差不多了,您只需要在
file.mtime
处使用file.attributes.mtime
即可。另外,我猜测问题中的代码只是一个示例,但为了使其执行,您还需要将用户名和密码传递给
start
并传递路径和模式到glob
。因此,一个有效的示例是:mtime
返回的值将是自纪元以来的秒数,因此您可能需要将其传递给 Time.at 将其转换为Time
对象。如果您好奇,可以以相同方式使用的其他属性包括:
permissions
uid
gid
size
Your example code is almost there, you just need to use
file.attributes.mtime
where you hadfile.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 toglob
. So a working example would be: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 aTime
object.In case you're curious, the other attributes available in the same way are:
permissions
uid
gid
size
atime
(time of last access)