Still new to Ruby and feel silly having to ask such a simple question,
but I can't find any examples of this on the web.
I have files in Dir1. I want to copy all the files, and only the files,
to Dir2. I don't always know the name of them or their extensions, I
just want them all copied.
Is there really nothing as simple as xcopy c:\dir1\*.* c:\dir2 ?? I
can't find a wildcard argument under FileUtils.cp. Am I missing the
painfully obvious solution here?
You may well get a simpler solution (and I'll be interested to see it myself),
but fwiw, here's how I roll with this kind of problem:
Dir.chdir( dir1 ) do
Dir.glob( "**/*" ).select { |f| File.file?( f ) }.each do |f|
dest = "#{dir2}/#{f}"
FileUtils.mkdir_p( File.dirname( dest ) )
FileUtils.cp( f, dest )
end
end
It's worth having a strategy for handling failures (remove partially copied dir2
in begin/rescue, or perhaps modify the block above to detect and remove existing
dest files, etc)
jonathan
···
On Wed, Apr 11, 2012 at 03:57:37AM +0900, Charlie B. wrote:
Still new to Ruby and feel silly having to ask such a simple question,
but I can't find any examples of this on the web.
I have files in Dir1. I want to copy all the files, and only the files,
to Dir2. I don't always know the name of them or their extensions, I
just want them all copied.
Is there really nothing as simple as xcopy c:\dir1\*.* c:\dir2 ?? I
can't find a wildcard argument under FileUtils.cp. Am I missing the
painfully obvious solution here?
I was afraid I'd have to resort to a loop to grab each file and move it.
I'm just shocked there isn't a one line solution to this. When your
scripting language is more difficult than a 20 year old DOS command,
that's ridiculous.
Is there really nothing as simple as xcopy c:\dir1\*.* c:\dir2 ?? I
can't find a wildcard argument under FileUtils.cp. Am I missing the
painfully obvious solution here?
FileUtils.cp works great if you know what you want to copy and if you
have relatively few items. If you have a folder with 50 files in it,
even if you knew their names it isn't practical to use the %w to copy
them all.
On Tue, Apr 10, 2012 at 3:19 PM, Charlie B. <lists@ruby-forum.com> wrote:
I was afraid I'd have to resort to a loop to grab each file and move it.
I'm just shocked there isn't a one line solution to this. When your
scripting language is more difficult than a 20 year old DOS command,
that's ridiculous.