![](/img/trans.png)
[英]Facing problems in copying files of type *.txt and *.xls in one folder to other folder using ruby script?
[英]Ruby Script: Copying files from one folder to multiple others, based on different names
我对编程和编码非常陌生,最近开始学习 Ruby。 我的问题是这样的:
我在一个文件夹中有一堆文件(大约 400 个),每个文件都有标识符,将它们分成 4 个单独的组。 我希望能够编写一个脚本来查看此文件夹,识别 4 个不同组中的文件,然后将文件复制到以标识符命名的四个单独文件夹中。 这是可能的吗?
如果是这样,那么是否可以根据其中标识符可以在文件夹中重叠的矩阵将文件复制到不同的文件夹中?
例如,假设每个文件属于四个不同的人:Bob、Harry、Tom、Steve。 (这些充当文件末尾的标识符)。
- Bob 可以拥有他自己和 Harry 的文件,但不能拥有其他两个。
- Harry 可以拥有他自己、Bob、Tom 的文件,但不能拥有 Steve。
- 汤姆可以拥有哈利和史蒂夫自己的文件,但不能拥有鲍勃。
- Steve 可以拥有他自己和 Tom 的文件,但不能拥有其他两个人的文件。
我可以根据上述参数编写一个脚本来查看文件并将它们复制到四个不同的文件夹中吗?
如果不是在 Ruby 中,是否有另一种编程语言可以做到这一点?
谢谢您的帮助!
这是一个让您入门的示例。 我将您的测试文件修改为ExampleA_Bob
形式,以便更轻松地获取标识符。
要进行测试,只需将file_testing.rb
和file_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)
文件所有者.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.