ruby - 如何在 ruby​​ 中复制目录结构,不包括某些文件扩展名

标签 ruby scripting

我想编写一个 ruby​​ 脚本来递归复制目录结构,但排除某些文件类型。因此,给定以下目录结构:

folder1
  folder2
    file1.txt
    file2.txt
    file3.cs
    file4.html
  folder2
  folder3
    file4.dll

我想复制这个结构,但不包含.txt.cs 文件。 因此,生成的目录结构应如下所示:

folder1
  folder2
    file4.html
  folder2
  folder3
    file4.dll

最佳答案

您可以使用查找 模块。这是一个代码片段:


require "find"

ignored_extensions = [".cs",".txt"]

Find.find(path_to_directory) do |file|
  # the name of the current file is in the variable file
  # you have to test it to see if it's a dir or a file using File.directory?
  # and you can get the extension using File.extname

  # this skips over the .cs and .txt files
  next if ignored_extensions.include?(File.extname(file))
  # insert logic to handle other type of files here
  # if the file is a directory, you have to create on your destination dir
  # and if it's a regular file, you just copy it.
end

关于ruby - 如何在 ruby​​ 中复制目录结构,不包括某些文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/711294/

相关文章:

ruby-on-rails - 在运行时而不是在 stub 声明中执行 rspec stub ?

ruby - 如何在 Ruby 中创建复制构造函数

ruby-on-rails - ActiveRecord 对象数组上的 Ruby Array#sort_by 似乎很慢

windows - 是否可以在脚本中重定向批处理文件的输出?

node.js - 安排 Heroku 每 10 分钟左右重新启动 dynos

ruby - 从两个字符串中删除重复的子字符串

ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT]

powershell - 删除数组的最后一个字符

java - Ivy、ant 和启动脚本

linux - 在远程服务器上执行两个命令并使用 shell 脚本将输出存储到本地服务器