1## Copyright (C) 2005, 2006, 2007, 2008, 2009 John W. Eaton
3## This file is part of Octave.
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
20## @deftypefn {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}, @var{force})
21## Copy the file @var{f1} to the new name @var{f2}. The name @var{f1}
22## may contain globbing patterns. If @var{f1} expands to multiple file
23## names, @var{f2} must be a directory. If @var{force} is given and equals
24## the string "f" the copy operation will be forced.
26## If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\
27## character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\
28## system-dependent error message, and @var{msgid} contains a unique\n\
29## message identifier.\n\
30## @seealso{glob, movefile}
33function [status, msg, msgid] = copyfile (f1, f2, force)
40 ## FIXME -- maybe use the same method as in ls to allow users control
41 ## over the command that is executed.
43 if (ispc () && ! isunix () && isempty (file_in_path (EXEC_PATH, "cp.exe")))
45 cmd = "cmd /C xcopy /E";
46 cmd_force_flag = "/Y";
49 cmd_force_flag = "-f";
52 if (nargin == 2 || nargin == 3)
54 if (! (ischar (f1) || iscellstr (f1)))
55 error ("copyfile: first argument must be a character string or a cell array of character strings");
59 error ("copyfile: second argument must be a character string");
62 if (nargin == 3 && strcmp (force, "f"))
63 cmd = cstrcat (cmd, " ", cmd_force_flag);
66 ## If f1 isn't a cellstr convert it to one.
71 ## If f1 has more than 1 element f2 must be a directory
72 isdir = (exist (f2, "dir") != 0);
73 if (length(f1) > 1 && ! isdir)
74 error ("copyfile: when copying multiple files, second argument must be a directory");
77 ## Protect the file name(s).
80 error ("copyfile: no files to move");
82 p1 = sprintf ("\"%s\" ", f1{:});
83 p2 = tilde_expand (f2);
85 if (isdir && length(p1) > max_cmd_line)
86 l2 = length(p2) + length (cmd) + 6;
88 p1 = sprintf ("\"%s\" ", f1{1});
90 while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 <
92 p1 = sprintf ("%s\"%s\" ", p1, f1{1});
96 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe")))
97 p1 = strrep (p1, "\\", "/");
98 p2 = strrep (p2, "\\", "/");
102 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2));
110 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe")))
111 p1 = strrep (p1, "\\", "/");
112 p2 = strrep (p2, "\\", "/");
116 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2));