itsh
itsh

Reputation: 1123

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. Can someone give me an example for better understanding?

Thanks

Upvotes: 3

Views: 1304

Answers (1)

Anthony
Anthony

Reputation: 15967

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:

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

Now we can pass an array into FilteUtils::cp_r

FileUtils.cp_r(files, destination)

Upvotes: 3

Related Questions