1 ## Copyright (C) 2005-2012 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}] =} movefile (@var{f1}, @var{f2})
21 ## @deftypefnx {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} movefile (@var{f1}, @var{f2}, 'f')
22 ## Move the file @var{f1} to the new name @var{f2}. The name @var{f1}
23 ## may contain globbing patterns. If @var{f1} expands to multiple file
24 ## names, @var{f2} must be a directory. If the force flag 'f' is given
25 ## then any existing files will be overwritten without prompting.
27 ## If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty
28 ## character strings. Otherwise, @var{status} is 0, @var{msg} contains a
29 ## system-dependent error message, and @var{msgid} contains a unique
30 ## message identifier.
31 ## @seealso{rename, copyfile}
34 function [status, msg, msgid] = movefile (f1, f2, force)
41 ## FIXME -- maybe use the same method as in ls to allow users control
42 ## over the command that is executed.
44 if (ispc () && ! isunix ()
45 && isempty (file_in_path (getenv ("PATH"), "mv.exe")))
48 cmd_force_flag = "/Y";
51 cmd_force_flag = "-f";
54 if (nargin == 2 || nargin == 3)
56 if (! (ischar (f1) || iscellstr (f1)))
57 error ("movefile: first argument must be a character string or a cell array of character strings");
61 error ("movefile: second argument must be a character string");
64 if (nargin == 3 && strcmp (force, "f"))
65 cmd = cstrcat (cmd, " ", cmd_force_flag);
68 ## If f1 isn't a cellstr convert it to one.
73 ## If f1 has more than 1 element f2 must be a directory
74 isdir = (exist (f2, "dir") != 0);
75 if (length(f1) > 1 && ! isdir)
76 error ("movefile: when moving multiple files, second argument must be a directory");
79 ## Protect the file name(s).
82 error ("movefile: no files to move");
84 p1 = sprintf ("\"%s\" ", f1{:});
85 p2 = tilde_expand (f2);
87 if (isdir && length(p1) > max_cmd_line)
88 l2 = length(p2) + length (cmd) + 6;
90 p1 = sprintf ("\"%s\" ", f1{1});
92 while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 <
94 p1 = sprintf ("%s\"%s\" ", p1, f1{1});
98 if (ispc () && ! isunix ()
99 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
100 p1 = strrep (p1, "\\", "/");
101 p2 = strrep (p2, "\\", "/");
105 [err, msg] = system (sprintf ("%s %s \"%s\"", cmd, p1, p2));
112 if (ispc () && ! isunix ()
113 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
114 p1 = strrep (p1, "\\", "/");
115 p2 = strrep (p2, "\\", "/");
119 [err, msg] = system (sprintf ("%s %s \"%s\"", cmd, p1, p2));