Questions about copying using FileUtils


#1

Hello,
Can someone please help me with the following script? It doesn’t work,
and yields the message below this script. My “puts” is just for testing,
to see if it sees the files. The “puts” works fine when I don’t include
the copy line just below it. I’m just copying original files to files of
the same name, with different extensions. I could as well be opening new
files of these new names, not just copying them.

As a related question, do all iterators, like my “each” below, require
an “end?”

Thanks a lot.


Dir.chdir(“c:/scripts/ruby/temp”)
require ‘fileutils’
include FileUtils::Verbose
psfiles = Dir.glob(’*.ps’)
psfiles.collect!{ |psfile| File.basename(psfile, ‘.ps’) }
psfiles.each do |psfile|
puts psfile
FileUtils.cp(“psfile”, psfile + “.pageinfo”)
end

gives me this . . .
test1
c:/ruby/lib/ruby/1.8/fileutils.rb:1182:in stat': No such file or directory - ps file (Errno::ENOENT) from c:/ruby/lib/ruby/1.8/fileutils.rb:1182:inlstat’
from c:/ruby/lib/ruby/1.8/fileutils.rb:1160:in stat' from c:/ruby/lib/ruby/1.8/fileutils.rb:1242:incopy_file’
from c:/ruby/lib/ruby/1.8/fileutils.rb:459:in copy_file' from c:/ruby/lib/ruby/1.8/fileutils.rb:383:incp’
from c:/ruby/lib/ruby/1.8/fileutils.rb:1377:in
fu_each_src_dest' from c:/ruby/lib/ruby/1.8/fileutils.rb:1393:infu_each_src_dest0’
from c:/ruby/lib/ruby/1.8/fileutils.rb:1375:in
fu_each_src_dest' from c:/ruby/lib/ruby/1.8/fileutils.rb:382:incp’
from c:/scripts/ruby/images/check_indexes.rb:10
from c:/scripts/ruby/images/check_indexes.rb:8


#2

“P” == Peter B. removed_email_address@domain.invalid writes:

P> Dir.chdir(“c:/scripts/ruby/temp”)
P> require ‘fileutils’
P> include FileUtils::Verbose
P> psfiles = Dir.glob(’*.ps’)
P> psfiles.collect!{ |psfile| File.basename(psfile, ‘.ps’) }
P> psfiles.each do |psfile|
P> puts psfile
P> FileUtils.cp(“psfile”, psfile + “.pageinfo”)
^^^^^^^^

Why do you use " here ? ruby search the file “psfile” and not the name
given in the variable `psfile’

Just write

FileUtils.cp(psfile, psfile + “.pageinfo”)

P> end

P> As a related question, do all iterators, like my “each” below,
require
P> an “end?”

This is do' which need anend’

You can write

1.times do |i|
puts i
end

or

1.times {|i|
puts i
}

Guy Decoux


#3

Peter B. wrote:

psfiles.each do |psfile|
puts psfile
FileUtils.cp(“psfile”, psfile + “.pageinfo”)
end

psfile is a variable and should not be quoted. Use

  FileUtils.cp(psfile, psfile + ".pageinfo")

#4

“P” == Peter B. removed_email_address@domain.invalid writes:

P> psfiles = Dir.glob(’*.ps’)
P> psfiles.collect!{ |psfile| File.basename(psfile, ‘.ps’) }
P> psfiles.each do |psfile|
P> puts psfile
P> FileUtils.cp(“psfile”, psfile + “.pageinfo”)

There is another problem, you have removed the suffix .ps with
File::basename, best to write it

  FileUtils.cp(psfile + ".ps", psfile + ".pageinfo")

Guy Decoux


#5

ts wrote:

“P” == Peter B. removed_email_address@domain.invalid writes:

P> psfiles = Dir.glob(’*.ps’)
P> psfiles.collect!{ |psfile| File.basename(psfile, ‘.ps’) }
P> psfiles.each do |psfile|
P> puts psfile
P> FileUtils.cp(“psfile”, psfile + “.pageinfo”)

There is another problem, you have removed the suffix .ps with
File::basename, best to write it

  FileUtils.cp(psfile + ".ps", psfile + ".pageinfo")

Guy Decoux

Now that worked! Thank you!


#6

ts wrote:

“P” == Peter B. removed_email_address@domain.invalid writes:

P> Dir.chdir(“c:/scripts/ruby/temp”)
P> require ‘fileutils’
P> include FileUtils::Verbose
P> psfiles = Dir.glob(’*.ps’)
P> psfiles.collect!{ |psfile| File.basename(psfile, ‘.ps’) }
P> psfiles.each do |psfile|
P> puts psfile
P> FileUtils.cp(“psfile”, psfile + “.pageinfo”)
^^^^^^^^

Why do you use " here ? ruby search the file “psfile” and not the name
given in the variable `psfile’

Just write

FileUtils.cp(psfile, psfile + “.pageinfo”)

P> end

P> As a related question, do all iterators, like my “each” below,
require
P> an “end?”

This is do' which need anend’

You can write

1.times do |i|
puts i
end

or

1.times {|i|
puts i
}

Guy Decoux

Thanks, Guy. I re-did that line, but get the same results.

FileUtils.cp(psfile, psfile + “.pageinfo”)

OK. So, it’s the “do” that needs the “end,” not the “each.”