0

I've run into what feels like a pretty basic error, but I can't find any documentation about what I'm struggling with. Here's the code:

require "fileutils"

def new_name(fn, dest = '/Volumes/External/Different\ Sublevel/Renamed', append = '_01')
    ext = File.extname(fn)
  File.join( dest, File.basename(fn, ext) + append + ext )
end

Dir[ '/Volumes/External/Example/Sublevels/**/*.xml' ].
select { |fn| File.file? fn }.
each   { |fn| FileUtils.cp fn, new_name(fn) }

All I'm trying to do is move some files (non-destructively) and append the filename. It works great on some local files, but I did multiple levels of ../../../ to get it to work. Is there something special about specifying external drives?

ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
Luke Patton
  • 63
  • 1
  • 8
  • Can you more exactly specify your problem ? Do you get some error message ? Btw. there is no difference between local filesystem and external volume mounted at the local fs. At least from fileutils perspective. – David Unric Mar 20 '13 at 00:50
  • I've been getting no error messages, just a blank terminal staring back at me. Simply no movement or anything is happening. I've passed it "puts" lines to make sure that it is reading, just nothing is being recognized or copied. – Luke Patton Mar 20 '13 at 01:04

1 Answers1

2

You are trying to pass string with escaped space character that is not interpreted inside apostrophes. You have to either omit the escape character

'/Volumes/External/Different Sublevel/Renamed'

or put it in double-quotes

"/Volumes/External/Different\ Sublevel/Renamed".

String created with apostrophes interprets only two escape sequences: backslash '\\' and apostrophe itself '\'' .

Details about Ruby strings at wikibooks.org

David Unric
  • 7,421
  • 1
  • 37
  • 65