Copying files using "ftools.rb" library

Copying files using "ftools.rb" library

Postby mgate » Wed Apr 19, 2017 11:57 am

I aim to copy a SketchUp file from one folder to another. I'm using the following method from the "ftools.rb" library:
Code: Select all
def copy(from, to, verbose = false)
    $stderr.print from, " -> ", catname(from, to), "\n" if verbose
    syscopy from, to
  end

  alias cp copy

when running my plugin, the file is copied correctly from the original folder to the destination folder but SketchUp crashes afterwards.

SketchUp Application has stopped working
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available.

I tried different options, i.e. keeping the same "to" file name as the "from", also changing it but SketchUp always crashes.
However, when using the move method of the "ftools.rb", it works perfectly with no problem even changing the "to" name:
Code: Select all
  def move(from, to, verbose = false)
    to = catname(from, to)
    $stderr.print from, " -> ", to, "\n" if verbose

    if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
      unlink to
    end
    fstat = stat(from)
    begin
      rename from, to
    rescue
      begin
        symlink readlink(from), to and unlink from
      rescue
   from_stat = stat(from)
   syscopy from, to and unlink from
   utime(from_stat.atime, from_stat.mtime, to)
   begin
     chown(fstat.uid, fstat.gid, to)
   rescue
   end
      end
    end
  end

  alias mv move

Any idea where the problem is? Thanks for your time.
Note: I use SU v8
0

mgate 
 

Re: Copying files using "ftools.rb" library

Postby Dan Rathbun » Fri Apr 21, 2017 3:03 am

(1) SU v8 uses Ruby 1.8.x which does not have Unicode character support on Windows. So any unicode characters in the pathnames could cause issues.

(2) Symbolic links require being logged into an administrator account on newer Windows version (I believe v6.0 [Vista] and higher.)

Lastly, you haven't said what kind of file you are copying. ?

Is the file currently in use by SketchUp or your extension ?
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Copying files using "ftools.rb" library

    Postby mgate » Fri Apr 21, 2017 10:00 am

    The goal is to create a copy of two existing template files (not opened) from "Resource" folder to a different path, the target files should have new names too (previously defined in a inputbox).
    templates:
    ...\Resources\XXX AT Template.skp
    ...\Resources\XXX AT Template.xlsx

    Here it is part of the script of my plugin, at the moment just trying to copy the skp file though:
    Code: Select all
    require 'ftools.rb'
    ...
    ...
    folder      = File.dirname( __FILE__ )
    resources   = "Resources"
    resource_name= "XXXXXX AT Template.skp"
    resource_file   = File.join(folder, resources, resource_name).tr("\\","/") # the script will be modified to select the directory via a UI.savepanel dialogue
    file_name   = "123456 AT aa.bb.skp"  # the real name will be picked via an inputbox
    desti_file   = File.join(folder, file_name).tr("\\","/")

    if File.exist?(resource_file)
     File.copy(resource_file, desti_file, verbose = true)
    else
     puts "SKETCHUP TEMPLATE FILE NOT FOUND!"
     return nil
    end
    ...
    ...

    The above script certainly creates the copy in the correct folder but SU crashes afterwards.

    As the move() doesn't crash, I tried to modify it as follow:
    Code: Select all
      def move(from, to, verbose = false)
        to = catname(from, to)
        $stderr.print from, " -> ", to, "\n" if verbose

    #    if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
    #      unlink to
    #    end
        fstat = stat(from)
        begin
    #     rename from, to
    #  rescue
          begin
          ln from, to and unlink from
          rescue
       from_stat = stat(from)
       syscopy from, to  and unlink from
       utime(from_stat.atime, from_stat.mtime, to)
       begin
         chown(fstat.uid, fstat.gid, to)
       rescue
       end
          end
        end
      end

      alias mv move

    by using above move(), the resource_file is kept and the desti_file is created but SU crashes too.

    I tried to use Fileutils.rb instead but there are lots of errors when open SU v.8 (I'm using !AdditionalPluginFolders.rb) and below doesn't work:
    Code: Select all
    FileUtils.copy_file(resource_file, desti_file, preserve = false, dereference = true)
    0

    mgate 
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    Who is online

    Users browsing this forum: measuredmove, solo and 4 guests

    Visit our sponsors: