In my Ruby scripting, there is probably no greater and chronic source of
irritation than way it copies files (something I do all the time). Most
of the time, I just fall back on a system command like cp file1 file2
because Fileutils.cp is so intransigent about syntax.
My problem is this. The script users enter a file name and a pathway as
follows:
pathName = “some_pathway”
fileName = “some_filename”
I simply want to copy that file to another directory. What syntax do I
need for Fileutils to copy the blasted file and quit issuing a billion
exceptions?
Intuitively, I’d say this should work:
Fileutils.cp( some_pathway+'/fileName', destDir )
Nope, it doesn’t like fileName. OK, I’ll protect it:
Fileutils.cp( some_pathway+'/#{fileName}', destDir )
Nope. OK, then:
Fileutils.cp( some_pathway+'/"#{fileName}'", destDir )
Nope.
(Try this another 50 ways, having it fail every time).
What am I doing wrong? Ruby is almost universally intuitive, but this
is one situation that makes me pull my hair out…
On Tue, Apr 23, 2013 at 4:00 PM, Thomas L. [email protected]
wrote:
I simply want to copy that file to another directory. What syntax do I
need for Fileutils to copy the blasted file and quit issuing a billion
exceptions?
You need to learn the effect of single and double quotes on string
interpolation, for one thing.
1.9.3 (main):0 > foo = “foo”
=> “foo”
1.9.3 (main):0 > “#{foo}”
=> “foo”
1.9.3 (main):0 > ‘#{foo}’
=> “#{foo}”
HTH,
Am 24.04.2013 01:00, schrieb Thomas L.:
I simply want to copy that file to another directory. What syntax do I
need for Fileutils to copy the blasted file and quit issuing a billion
exceptions?
That has really nothing to do with FileUtils.
You need to distinguish between variables (e.g. pathName)
and string literals (e.g. ‘some_pathway’). Also you need
to know that string interpolation with #{} only works
inside double quotes.
These should work:
- ‘some_pathway’ + ‘/’ + ‘some_filename’
- ‘some_pathway’ + ‘/’ + fileName
- pathName + ‘/’ + fileName
- “#{pathName}/#{fileName}”
Tip: use IRB to try out what these expressions return!
Intuitively, I’d say this should work:
Fileutils.cp( some_pathway+'/fileName', destDir )
only works when some_pathway is a variable and the
filename is ‘fileName’
Nope, it doesn’t like fileName. OK, I’ll protect it:
Fileutils.cp( some_pathway+'/#{fileName}', destDir )
only works when the filename is literally ‘/#{filename}’,
and how likely is that…
Nope. OK, then:
Fileutils.cp( some_pathway+'/"#{fileName}'", destDir )
that’s a plain syntax error, since the quotes do not match
(you have the string literal ‘/"#{fileName}’ and a single "
that lacks a closing ")
Nope.
You should read up on some basics about string literals
and using variables.
Note also that it is common in the Ruby community to use
snail case instead of camel case for variable names
(path_name' instead of
pathName’).
On 27/04/2013, at 10:44 AM, Thomas L. [email protected] wrote:
Dir.mkdir( “move_dir” )
Dir.chdir( move_dir )
You create a directory called “move_dir”.
Then you cd into the directory pointed to by the variable move_dir which
at that point is nil, thus the error no such file or directory called
nil.
Henry
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.
Two things:
(1) I still have trouble with this. I’m following Doug’s example, and
I’m getting this exception about the target directory. The script
output:
The file to be moved is
“/home/t3905/codes/xcobra/ddec12/nhyd_mod/build/new_build/mod_files/ANF1_diff_wb”
The destination file is
“/home/t3905/codes/xcobra/ddec12/nhyd_mod/build/new_build/hp/file_diff/source_diff/ANF1_diff_wb”
So the current file plus path and destination file plus path are all
set. Now I simply do the FileUtils.cp (or .mv) using the regular
syntax. Naturally, it does not work:
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/codes/xcobra/ddec12/nhyd_mod/build/new_build/hp/file_diff/source_diff/ANF1_diff_wb
Why is it puking about the destination file not existing? That’s why
I’m trying to move it…
(2) I have no idea why I should continue fighting this, when I can
simply utilize a system command that I never have to fight with.
Using a UNIX cp works simpler and easier on both Linux and other UNIX
platforms, and I never have any problems with it. Never.
I love Ruby, but loathe FileUtils.
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}”
Am 14.05.2013 05:05, schrieb Thomas L.:
I love Ruby, but loathe FileUtils.
Your problems had nothing to do with FileUtils, but with
misconceptions about variables, strings, string interpolation, …!
So don’t blame the library.
If you want to make sure your target directory exists, you can
use `FileUtils.mkdir_p’ before trying to move.
(BTW: You would have the same problem in a UNIX shell.)
On Mon, May 13, 2013 at 10:04 PM, Thomas L.
[email protected]wrote:
The destination file is
/home/t3905/codes/xcobra/ddec12/nhyd_mod/build/new_build/hp/file_diff/source_diff/ANF1_diff_wb
Why is it puking about the destination file not existing? That’s why
I’m trying to move it…
My guess is that the directory …/hp/file_diff/source_diff/ doesn’t
exist.