Need help improving this FileUtils.cp of an array of destinations

Hi

Got any suggestion on how to improve this Filecopy code snippet below?

$files.each_with_progress do | val |
$files.each do |element|
FileUtils.cp("#{$old_home}/#{$sName}", “#{Dir.pwd}/#{element}”)
end
end

Some guidelines to the variables in the code:

The ‘$files’ variable contains an array of destinations(paths), about
139 different paths at the moment.

$old_home and $sName is the sourcepath and sourcefilename of the file to
be copyed.

Basically, the code replaces one specific file with 139 other files with
the same name and extension in 139 different location. Or you could say,
it updates 139 files with copy/replace.

In my point of view, it seems like the code is trying to copy 139 files
at the same time to 139 different location withoutcompleting one
“copyaction” at the time before copying another file. Wouldn’t it be
more efficient to wait for one file to be completely replaced before
starting another “copyaction”. Am I wrong? Got any suggestion on how to
improve this code?

Please ask if anything was unclear.

Thanks in advanced!

Well I played a little with the code

$files = [‘path1/’,‘path2/’,‘path3/’]

$old_home = ‘Old_Home’
$sName = ‘S_Name’

and changed the FileCopy to

puts “#{$old_home}/#{$sName}”, “#{Dir.pwd}/#{element}”

and this is what the output looks like

Old_Home/S_Name /tmp/path1/
Old_Home/S_Name /tmp/path2/
Old_Home/S_Name /tmp/path3/
Old_Home/S_Name /tmp/path1/
Old_Home/S_Name /tmp/path2/
Old_Home/S_Name /tmp/path3/
Old_Home/S_Name /tmp/path1/
Old_Home/S_Name /tmp/path2/
Old_Home/S_Name /tmp/path3/

The code you gave is broken and is not doing what you think it is.

Hi!

Thanks for quick reply.

But im not sure im following your statement.

I did as you have done.

$files = [‘path1/’,‘path2/’,‘path3/’]

$old_home = ‘Old_Home’
$sName = ‘S_Name’

$files.each do |element|

puts “#{$old_home}/#{$sName}”, “#{Dir.pwd}/#{element}”

end

Old_Home/S_Name C:/path1/
Old_Home/S_Name C:/path2/
Old_Home/S_Name C:/path3/

please explain in depth

This is the code you posted

$files.each_with_progress do | val |
$files.each do |element|
FileUtils.cp("#{$old_home}/#{$sName}", “#{Dir.pwd}/#{element}”)
end
end

This is how I changed it

$files.each_with_progress do | val |
$files.each do |element|
puts “#{$old_home}/#{$sName}”, “#{Dir.pwd}/#{element}”
end
end

On Thu, Nov 8, 2012 at 11:08 AM, Peter H. <
[email protected]> wrote:

This is the code you posted

$files.each_with_progress do | val |
$files.each do |element|
FileUtils.cp("#{$old_home}/#{$sName}", “#{Dir.pwd}/#{element}”)
end
end

I do especially wonder what val is used for and why there seem to be two
iterations through $files - this is O(n*n).

Your description looks contradictory to me: on one hand you say that
$files
holds destinations and then you say that one file is replaced by 139
files
(which would mean $files contain sources).

To copy a file into destinations

src = File.join old_home, sName

$files.each do |dst|
FileUtils.cp src, dst
end

Kind regards

robert