Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 检查目录中是否存在数组中的某些文件_Ruby_Chef Infra - Fatal编程技术网

Ruby 检查目录中是否存在数组中的某些文件

Ruby 检查目录中是否存在数组中的某些文件,ruby,chef-infra,Ruby,Chef Infra,我编写了以下代码来重命名目录中的一些文件(从'rb'到'origin'): 当我第二次运行它时,我得到一个错误,这些文件不存在,这是预期的 如何检查这些文件是否存在: if ::File.exist?("C:\\Test\\*.origin") Chef::Log.info("########## Your files are already renamed ############") else my_code end 或者以另一种方式(可能是在循环中检查)?这是我经过多次尝试后的解

我编写了以下代码来重命名目录中的一些文件(从
'rb'
'origin'
):

当我第二次运行它时,我得到一个错误,这些文件不存在,这是预期的

如何检查这些文件是否存在:

if ::File.exist?("C:\\Test\\*.origin")
  Chef::Log.info("########## Your files are already renamed ############")
else
  my_code
end

或者以另一种方式(可能是在循环中检查)?

这是我经过多次尝试后的解决方案:

origin_files = ['test1.rb', 'test2.rb']
dir_path = "C:\\Test"

ruby_block "Rename file" do
    block do 
        for filename in origin_files
            newname = filename.split(".")[0] + '.origin'
            if ::File.exist?("#{dir_path}\\#{newname}")
                Chef::Log.info("### Your file: #{newname} already renamed ###")
            else
                ::File.rename("#{dir_path}\\#{filename}", "#{dir_path}\\#{newname}")
            end
        end     
    end             
end

您可以像下面的代码一样检查此文件

files = ["test1.rb", "test2.rb"]

path = "C:/test"

files.each { |file_name|
  base_name = File.basename(file_name, ".rb")
  new_name = base_name + ".origin"
  if File.exists?(File.join(path, new_name))
    Chef::Log.info("########## Your file #{base_name} are already renamed ############")
  elsif File.exists?(File.join(path, file_name))
    File.rename(File.join(path, file_name), File.join(path, new_name))
    Chef::Log.info("########## Your file #{base_name} was renamed ############")
  else
    Chef::Log.info("########## Your file #{file_name} not exist ############")
  end
}

为此,您可以使用厨师长守卫。它会在chef客户端运行时自动记录事件

ruby\u块xxx(由于非\u if而跳过)

以下是一个例子:

origin_files = ['test1.rb', 'test2.rb']
dir_path = "C:\\Test"

origin_files.each do | old_file |
    base_name = File.basename(old_file, ".rb")
    newfile = base_name + ".origin"
    ruby_block "rename #{old_file}" do
      block do
        File.rename(File.join(dir_path, old_file), File.join(dir_path, newfile))
      end
      not_if { File.exist?("#{dir_path}\\#{newfile}") }
    end
end

您好,sawa,谢谢您的回复,请您将其写入我的代码中(我尝试了此解决方案,但无效)。感谢您使用
File.basename('test1.rb','.rb')获取不带扩展名的文件名。
origin_files = ['test1.rb', 'test2.rb']
dir_path = "C:\\Test"

origin_files.each do | old_file |
    base_name = File.basename(old_file, ".rb")
    newfile = base_name + ".origin"
    ruby_block "rename #{old_file}" do
      block do
        File.rename(File.join(dir_path, old_file), File.join(dir_path, newfile))
      end
      not_if { File.exist?("#{dir_path}\\#{newfile}") }
    end
end