From: Douglas Seifert Date: 2013-04-28T06:04:27+09:00 Subject: Re: copying files syntax with FileUtils.rb (grr.) --047d7b41cc1829652704db5dffef Content-Type: text/plain; charset=ISO-8859-1 Besides what Henry said, it seems like you go through some extreme gymnastics to create the path2 variable's value. I'd do something like this: require 'fileutils' filename = "just_a_file" target_dir = "move_dir" path1 = Dir.getwd Dir.mkdir target_dir unless File.directory? target_dir path2 = File.join path1, target_dir puts "Copying '#{path1 }/#{filename}' -> '#{path2 }/#{filename}'" FileUtils.cp "#{path1 }/#{filename}", "#{path2 }/#{filename}" On Fri, Apr 26, 2013 at 3:44 PM, Thomas Luedeke wrote: > OK, I just spent about an hour fooling around and looking up string > literals, and I'm still having lots of trouble with the *syntax* for > this utility. I freely admit it may be that I simply don't understand > this. > > I want an example of how a local variable pathway and a local variable > name can be used in FileUtils.cp. Because I cannot get it to work > cleanly. > > filename = "just_a_file" > > path1 = Dir.getwd > > Dir.mkdir( "move_dir" ) > Dir.chdir( move_dir ) > path2 = Dir.getwd > Dir.chdir( path1 ) > > FileUtils.cp( "#{path1 }/#{filename}", "#{path2 }/#{filename}" ) > > This should work cleanly, since all local variables are within double > brackets. It actually does move the file, but still pukes out an > exception: > > Exception `Errno::ENOENT' at > /home/t3905/ruby/ruby-1.8.7-p22/lib/ruby/site_ruby/1.8/fileutils.rb:1420 > - No such file or directory - > /home/t3905/test/ruby_move/move_test/just_a_file > > So what am I doing wrong?? I completely fail to understand the problem > here. > > -- > Posted via http://www.ruby-forum.com/. > > --047d7b41cc1829652704db5dffef Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Besides what Henry said, it seems like you go through some= extreme gymnastics to create the path2 variable's value. =A0I'd do= something like this:

require 'fileutils'
filename =3D "just_a_file"
target_dir =3D &q= uot;move_dir"

path1 =3D Dir.getwd
D= ir.mkdir target_dir unless File.directory? target_dir

<= div> path2 =3D File.join path1, target_dir

puts "C= opying '#{path1 }/#{filename}' -> '#{path2 }/#{filename}'= ;"
FileUtils.cp "#{path1 }/#{filename}", "#{p= ath2 }/#{filename}"



On Fri, Apr 26, 2013 at 3:44 PM, Thomas Luedeke &= lt;lists@ruby-for= um.com> wrote:
OK, I just spent about an hour fooling aroun= d and looking up string
literals, and I'm still having lots of trouble with the *syntax* for this utility. =A0I freely admit it may be that I simply don't understan= d
this.

I want an example of how a local variable pathway and a local variable
name can be used in FileUtils.cp. =A0Because I cannot get it to work
cleanly.

filename =3D "just_a_file"

path1 =3D Dir.getwd

Dir.mkdir( "move_dir" )
Dir.chdir( move_dir )
path2 =3D Dir.getwd
Dir.chdir( path1 )

FileUtils.cp( "#{path1 }/#{filename}", "#{path2 }/#{filename= }" )

This should work cleanly, since all local variables are within double
brackets. =A0It actually does move the file, but still pukes out an
exception:

Exception `Errno::ENOENT' at
/home/t3905/ruby/ruby-1.8.7-p22/lib/ruby/site_ruby/1.8/fileutils.rb:1420 - No such file or directory -
/home/t3905/test/ruby_move/move_test/just_a_file

So what am I doing wrong?? =A0I completely fail to understand the problem here.

--047d7b41cc1829652704db5dffef--