如何在Ruby中的服务器上创建文件夹?

时间:2016-08-01 03:54:29

标签: ruby scp

我正在编写一个脚本,使用模块net/scp将文件夹从客户端上传到服务器但我不知道如果服务器上尚不存在,则使用ruby自动创建新文件夹。我该怎么做?如何使用net/scp在服务器上运行任何Ruby脚本?

这是我到目前为止的代码:

def upload(fileName_upload,pathLocalFolder,pathRemoteFolder)
  pathLocalFolder=pathLocalFolder + '/'+fileName_upload
  Net::SCP.start($host, $username, :password => $pass_Word) do |scp|
  scp.upload! pathLocalFolder, pathRemoteFolder
  channel = scp.upload(pathLocalFolder, pathRemoteFolder)
  channel.wait
end

def uploadFolder(pathLocalFolder,nameFolderInServer)
  # I want to create a folder on the server, but I don't know how to do it
  #Dir.mkdir(nameFolderInServer) unless File.exists?(nameFolderInServer)
  Dir.glob(pathLocalFolder) do |item|
    name=File.basename item,''
    if File.directory?(name)
      uploadFolder(pathLocalFolder+'/'+name,name)
    end
    if File.file?(name)
      upload(name,pathLocalFolder,$remote_Path+'/'+nameFolderInServer)
    end
  end
end

1 个答案:

答案 0 :(得分:0)

使用FileUtils.mkdir_p。没有必要检查它是否已经存在,因为如果给定目录已经存在,此方法将不执行任何操作。