小编Lau*_*ona的帖子

如果目录已存在,则在 Ruby 上通过 SFTP 创建目录失败

仅当目录不存在时,如何才能通过 SFTP 在 Ruby 上创建目录?

我现在有以下代码:

Net::SFTP.start( ip, id, :password => pass, :port=> port ) do |sftp|
    sftp.mkdir! remotePath
    sftp.upload!(localPath + localfile, remotePath + remotefile)
end
Run Code Online (Sandbox Code Playgroud)

我第一次创建目录没有问题,但它会尝试重新创建相同的目录,即使它已经存在并且它会抛出一个错误。

有谁知道如何做到这一点?

在使用 fileutils 时,有这样的代码:

 FileUtils.mkdir_p(remotePath) unless File.exists?(remotePath)
Run Code Online (Sandbox Code Playgroud)

有什么办法可以通过 SFTP 做同样的事情吗?

ruby directory sftp ruby-on-rails fileutils

5
推荐指数
1
解决办法
2728
查看次数

标签 统计

directory ×1

fileutils ×1

ruby ×1

ruby-on-rails ×1

sftp ×1