Ruby 脚本:根据不同的名称将文件从一个文件夹复制到多个其他文件夹

Ruby Script: Copying files from one folder to multiple others, based on different names

我对编程和编码很陌生,最近才开始学习 Ruby。我的问题是:

我在一个文件夹中有一堆文件(大约 400 个),每个文件都有将它们分为 4 个独立组的标识符。我希望能够编写一个脚本来查看此文件夹,识别 4 个不同组中的文件,然后将文件复制到以标识符命名的四个单独的文件夹中。这可能吗?

如果是这样,是否可以根据哪个标识符可以在文件夹中重叠的矩阵将文件复制到不同的文件夹中?

例如,假设每个文件属于四个不同的人:Bob、Harry、Tom 和 Steve。 (这些充当文件末尾的标识符)。

  1. Bob can have files from himself, and Harry but not the other two.
  2. Harry can have files from himself, Bob, Tom, but not Steve.
  3. Tom can have files from himself Harry and Steve, but not Bob.
  4. Steve can have files from himself and Tom but not the other two.

我能否根据上述参数编写一个脚本来查看文件并将它们复制到四个不同的文件夹中?

如果 Ruby 没有,是否有其他编程语言可以做到这一点?

感谢您的帮助!

这是一个让您入门的示例。我将您的测试文件修改为 ExampleA_Bob 形式,以便更容易获得标识符。

要进行测试,只需将 file_testing.rbfile_owner.rb 放入一个文件夹中,然后将 运行 放入 ruby file_testing.rb。它将制作测试文件,并根据是否允许他们查看它们将它们复制到每个人的文件夹中。

file_testing.rb

require "fileutils"
require_relative "file_owner"

# -----------------------------------------------------------------------------
# ---Helper Functions----------------------------------------------------------
# -----------------------------------------------------------------------------
def create_test_files(directory_for_files, file_names)
  FileUtils.mkdir_p(directory_for_files)
  file_names.each{ |file_name|
    out_file = File.new("#{directory_for_files}#{file_name}", "w")
    out_file.puts("Testing #{file_name}")
    out_file.close
  }
end

def create_file_owners(file_owner_permissions, path_to_files)
  file_owners = []
  file_owner_permissions.each{ |owner_name, owner_permissions|
    file_owners.push(FileOwner.new(owner_name.to_s, owner_permissions, path_to_files))
  }

  return file_owners
end

def parse_file_identifier(file_name)
  split_name = file_name.split("_")
  return split_name[-1]
end

def sort_files(file_owners, path_to_files)
  Dir.foreach(path_to_files) do |file|
    next if file == "." or file == ".."
    next if File.directory?(path_to_files + file)

    file_owners.each{ |owner|
      file_identifier = parse_file_identifier(file)
      owner.copy_file_if_allowed(path_to_files + file, file_identifier)
    }
  end
end

# -----------------------------------------------------------------------------
# ---Main----------------------------------------------------------------------
# -----------------------------------------------------------------------------
path_to_files = "./test_files/"
file_names = ["ExampleA_Bob", "ExampleB_Bob", "ExampleC_Bob", "ExampleA_Harry", "ExampleB_Harry", "ExampleC_Harry", "ExampleA_Tom", "ExampleB_Tom", "ExampleC_Tom", "ExampleA_Steve", "ExampleB_Steve", "ExampleC_Steve"]
create_test_files(path_to_files, file_names)

file_owner_permissions = {
  "Bob": ["Harry"],
  "Harry": ["Bob", "Tom"],
  "Tom": ["Harry", "Steve"],
  "Steve": ["Tom"]
}
file_owners = create_file_owners(file_owner_permissions, path_to_files)
sort_files(file_owners, path_to_files)

file_owner.rb

require 'fileutils'
class FileOwner
  attr_accessor :name
  attr_accessor :permissions

  def initialize(name, permissions, path_to_files)
    @name = name
    @permissions = permissions.push(name)
    @personal_folder = path_to_files + name
    ensure_personal_folder_exists()
  end

  public
  def copy_file_if_allowed(file_path, file_identifier)
    if @permissions.include? file_identifier
      add_file_to_personal_folder(file_path)
    end
  end

  private
  def ensure_personal_folder_exists()
    FileUtils.mkdir_p(@personal_folder)
  end

  def add_file_to_personal_folder(file_path)
    FileUtils.cp(file_path, @personal_folder)
  end
end