1
## Copyright (C) 2005-2013 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})
21
## @deftypefnx {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}, 'f')
22
## Copy the file @var{f1} to the destination @var{f2}.
24
## The name @var{f1} may contain globbing patterns. If @var{f1} expands to
25
## multiple file names, @var{f2} must be a directory.
26
## when the force flag @qcode{'f'} is given any existing files will be
27
## overwritten without prompting.
29
## If successful, @var{status} is 1, and @var{msg}, @var{msgid} are empty
30
## character strings (""). Otherwise, @var{status} is 0, @var{msg} contains a
31
## system-dependent error message, and @var{msgid} contains a unique message
32
## identifier. Note that the status code is exactly opposite that of the
33
## @code{system} command.
34
## @seealso{movefile, rename, unlink, delete, glob}
37
function [status, msg, msgid] = copyfile (f1, f2, force)
39
if (nargin < 2 || nargin > 3)
48
## FIXME: maybe use the same method as in ls to allow users control
49
## over the command that is executed.
51
if (ispc () && ! isunix ()
52
&& isempty (file_in_path (getenv ("PATH"), "cp.exe")))
54
cmd = "cmd /C xcopy /E";
55
cmd_force_flag = "/Y";
58
cmd_force_flag = "-f";
62
if (! (ischar (f1) || iscellstr (f1)))
63
error ("copyfile: F1 must be a character string or a cell array of character strings");
67
error ("copyfile: F2 must be a character string");
70
if (nargin == 3 && strcmp (force, "f"))
71
cmd = [cmd " " cmd_force_flag];
74
## If f1 isn't a cellstr convert it to one.
79
## If f1 has more than 1 element f2 must be a directory
80
isdir = (exist (f2, "dir") != 0);
81
if (length (f1) > 1 && ! isdir)
82
error ("copyfile: when copying multiple files, F2 must be a directory");
85
## Protect the file name(s).
88
error ("copyfile: no files to move");
90
p1 = sprintf ('"%s" ', f1{:});
91
p2 = tilde_expand (f2);
93
if (isdir && length (p1) > max_cmd_line)
94
l2 = length (p2) + length (cmd) + 6;
95
while (! isempty (f1))
96
p1 = sprintf ('"%s" ', f1{1});
99
&& (length (p1) + length (f1{1}) + l2 < max_cmd_line))
100
p1 = sprintf ('%s"%s" ', p1, f1{1});
104
if (ispc () && ! isunix ()
105
&& ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
106
p1 = strrep (p1, '\', '/');
107
p2 = strrep (p2, '\', '/');
111
[err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2));
119
if (ispc () && ! isunix ()
120
&& ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
121
p1 = strrep (p1, '\', '/');
122
p2 = strrep (p2, '\', '/');
126
[err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2));