我想编写一个 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/