I want to use each index of the array and loop it in such a way that the
path names can be passed into Dir so that i can carry out multiple
copies from different sources. unfortunately, it showed error while
trying to run the script. Any suggestions?
You seem to get confused with the indexes, but the idiomatic Ruby code
would rather look like
dest = “u:/…”
sources = %w{ c:/movetest/testing c:/ … } # unless you have spaces
in the pathes
sources.each do | source |
Dir.chdir source
puts source
FileUtiles.cp_r Dir.glob(…), dest # if this is what you really want
end
in the pathes
sources.each do | source |
Dir.chdir source
puts source
FileUtiles.cp_r Dir.glob(…), dest # if this is what you really want
end
Inside the block I prefer
Dir.chdir source do
puts source
FileUtiles.cp_r Dir.glob(...), dest
end
which will restore the original current directory. That way, you can
more easily work with relative paths and it might save you from
surprises when in a completely unrelated location of a larger script
current dir is no longer the one that it was on program start.
in the pathes
sources.each do | source |
Dir.chdir source
puts source
FileUtiles.cp_r Dir.glob(…), dest # if this is what you really want
end
Inside the block I prefer
Dir.chdir source do
puts source
FileUtiles.cp_r Dir.glob(...), dest
end
which will restore the original current directory. That way, you can
more easily work with relative paths and it might save you from
surprises when in a completely unrelated location of a larger script
current dir is no longer the one that it was on program start.
Kind regards
robert
The code is actually feasible but for a typo mistake!(Cause there wasnt
a dir named U:\test1) Here’s the edited code:
But is there anyway whereby, I can write all the ‘print’ to a log file
for reference, just in case there might be an error in copying? And if
possible have some conditions to test if the files did really copy over?
Thanks.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.