octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: use of system to copy files in pkg.m


From: Bill Denney
Subject: Re: use of system to copy files in pkg.m
Date: Tue, 10 Oct 2006 14:25:21 -0400 (EDT)

On Tue, 10 Oct 2006, John W. Eaton wrote:

Of these, nine are for copying files.  I think we should at least
encapsulate this in a separate function.  Matlab has copyfile.  I have
the following version of copyfile for Octave that also uses system
(I've omitted the copyright and help text here):


Here is a version that may work in windows, too:

function [status, msg, msgid] = copyfile (f1, f2, force)

  status = true;
  msg = "";
  msgid = "";

  if ~ (exist (f1, "file") || exist (f1, "dir"))
    error ("copyfile: f1 must be a file or directory");
  elseif ~ (ischar (f2) && (size(f2, 1) == 1))
    error ("copyfile: f2 must be a single lined string");
  endif

  if (nargin == 2 || nargin == 3)
    if (isunix ())
      ## unix copy command
      if (nargin == 3 && strcmp (force, "f"))
        cmd = "/bin/cp -rf";
      else
        cmd = "/bin/cp -r";
      endif
    else
      ## windows copy command
      cmd = "copy /y";
      if (nargin == 3 && strcmp (force, "f"))
        fstat = stat(f2);
        if (~ strcmp('w', fstat.modestr(3)))
          ## does this correctly check for readonly on windows?
          ## file is readonly, change it to readwrite
          [err, msg] = system (sprintf ("attrib -r %s", f2));
          if (err < 0)
            status = false;
            msgid = sprintf("attrib failed to make '%s' writable", f2);
          endif
        endif
      endif
    endif
    [err, msg] = system (sprintf ("%s %s %s", cmd, f1, f2));
    if (err < 0)
      status = false;
      msgid = "copyfile";
    endif
  else
    print_usage ();
  endif

endfunction

I don't have a windows version of octave now, but the general idea should be right.

I also have movefile:

Here is a similar edit that should work for windows, too:

function [status, msg, msgid] = movefile (f1, f2, force)

  status = true;
  msg = "";
  msgid = "";

  if ~ (exist (f1, "file") || exist (f1, "dir"))
    error ("movefile: f1 must be a file or directory");
  elseif ~ (ischar (f2) && (size(f2, 1) == 1))
    error ("movefile: f2 must be a single lined string");
  endif

  force = (nargin == 3 && strcmp (force, "f"));

  if (nargin == 2 || nargin == 3)
    if (isunix ())
      ## unix move command
      if force
        cmd = "/bin/mv -f";
      else
        cmd = "/bin/mv";
      endif
    else
      ## windows copy command
      cmd = "move /y";
      if force
        fstat = stat(f2);
        if (~ strcmp('w', fstat.modestr(3)))
          ## does this correctly check for readonly on windows?
          ## file is readonly, change it to readwrite
          [err, msg] = system (sprintf ("attrib -r %s", f2));
          if (err < 0)
            status = false;
            msgid = sprintf("attrib failed to make '%s' writable", f2);
          endif
        endif
      endif
    endif
    [err, msg] = system (sprintf ("%s %s %s", cmd, f1, f2));
    if (err < 0)
      status = false;
      msgid = "movefile";
    endif
  else
    print_usage ();
  endif

endfunction

Bill


reply via email to

[Prev in Thread] Current Thread [Next in Thread]