简体   繁体   English

复制除Ruby中的少数文件和文件夹

[英]Copy all files and folders except few in Ruby

I was wondering if I can use FileUtils.cp_r method to copy all the files and directories from source to target except .tar files. 我想知道我是否可以使用FileUtils.cp_r方法将所有文件和目录从源复制到目标,除了.tar文件。 Can someone give me an example for better understanding? 有人可以给我一个更好理解的例子吗?

Thanks 谢谢

Sure but you'd have to implement some kind of filter first like this: 当然,你必须首先实现某种过滤器,如下所示:

[8] pry(main)> Dir.glob("**/*")
=> ["bin", "CODE_OF_CONDUCT.md", "Gemfile", "Gemfile.lock", "hello.tar", "lib", "LICENSE.txt", "mygem.gemspec", "Rakefile", "README.md", "spec"]

That gave us all the files in that directory and subsequent directories(thanks ndn for that tip), now let's filter out that hello.tar : 这给了我们该目录和后续目录中的所有文件(感谢ndn的提示),现在让我们过滤掉hello.tar

files = Dir.glob("**/*").reject { |file| file.end_with?(".tar") }

Now we can pass an array into FilteUtils::cp_r 现在我们可以将数组传递给FilteUtils::cp_r

FileUtils.cp_r(files, destination)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM