Forum: Ruby Questions about copying using FileUtils

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-20 16:42
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:in `lstat'
        from c:/ruby/lib/ruby/1.8/fileutils.rb:1160:in `stat'
        from c:/ruby/lib/ruby/1.8/fileutils.rb:1242:in `copy_file'
        from c:/ruby/lib/ruby/1.8/fileutils.rb:459:in `copy_file'
        from c:/ruby/lib/ruby/1.8/fileutils.rb:383:in `cp'
        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:in
`fu_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:in `cp'
        from c:/scripts/ruby/images/check_indexes.rb:10
        from c:/scripts/ruby/images/check_indexes.rb:8
--------------------------------------------------------------------------------
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-04-20 16:59
(Received via mailing list)
>>>>> "P" == Peter Bailey <pbailey@bna.com> 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 an `end'

 You can write

   1.times do |i|
      puts i
   end

 or

   1.times {|i|
      puts i
   }



Guy Decoux
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2006-04-20 17:01
Peter Bailey 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")
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-04-20 17:05
(Received via mailing list)
>>>>> "P" == Peter Bailey <pbailey@bna.com> 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
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-20 17:20
ts wrote:
>>>>>> "P" == Peter Bailey <pbailey@bna.com> 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 an `end'
>
>  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."
B58c6eef325656d513d26e2c3ae6bfd9?d=identicon&s=25 Peter Bailey (peterbailey)
on 2006-04-20 17:22
ts wrote:
>>>>>> "P" == Peter Bailey <pbailey@bna.com> 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!
This topic is locked and can not be replied to.