6047
|
1 ## Copyright (C) 2005 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
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 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
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. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} movefile (@var{f1}, @var{f2}) |
|
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. |
|
25 ## |
|
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} |
|
31 ## @end deftypefn |
|
32 |
|
33 function [status, msg, msgid] = movefile (f1, f2, force) |
|
34 |
6679
|
35 max_cmd_line = 1024; |
6047
|
36 status = true; |
|
37 msg = ""; |
|
38 msgid = ""; |
|
39 |
6210
|
40 ## FIXME -- maybe use the same method as in ls to allow users control |
|
41 ## over the command that is executed. |
|
42 |
6679
|
43 if (ispc () && ! isunix () && isempty (file_in_path (EXEC_PATH, "mv.exe"))) |
6233
|
44 ## Windows. |
6210
|
45 cmd = "cmd /C move"; |
|
46 cmd_force_flag = "/Y"; |
|
47 else |
|
48 cmd = "mv"; |
|
49 cmd_force_flag = "-f"; |
|
50 endif |
|
51 |
6047
|
52 if (nargin == 2 || nargin == 3) |
6233
|
53 ## Input type check. |
|
54 if (! (ischar (f1) || iscellstr (f1))) |
6679
|
55 error ("movefile: first argument must be a character string or a cell array of character strings"); |
6233
|
56 endif |
|
57 |
|
58 if (! ischar (f2)) |
6679
|
59 error ("movefile: second argument must be a character string"); |
6233
|
60 endif |
|
61 |
6047
|
62 if (nargin == 3 && strcmp (force, "f")) |
6210
|
63 cmd = strcat (cmd, " ", cmd_force_flag); |
6047
|
64 endif |
6069
|
65 |
6233
|
66 ## If f1 isn't a cellstr convert it to one. |
|
67 if (ischar (f1)) |
|
68 f1 = cellstr (f1); |
6069
|
69 endif |
6233
|
70 |
6679
|
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 ("movefile: when moving multiple files, second argument must be a directory"); |
|
75 endif |
|
76 |
6233
|
77 ## Protect the file name(s). |
|
78 f1 = glob (f1); |
6679
|
79 p1 = sprintf ("\"%s\" ", f1{:}); |
|
80 p2 = tilde_expand (f2); |
6069
|
81 |
6679
|
82 if (isdir && length(p1) > max_cmd_line) |
|
83 l2 = length(p2) + length (cmd) + 6; |
|
84 while (! isempty(f1)) |
|
85 p1 = sprintf ("\"%s\" ", f1{1}); |
|
86 f1(1) = []; |
|
87 while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 < |
|
88 max_cmd_line)) |
|
89 p1 = sprintf ("%s\"%s\" ", p1, f1{1}); |
|
90 f1(1) = []; |
|
91 endwhile |
|
92 |
|
93 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe"))) |
|
94 p1 = strrep (p1, "\\", "/"); |
|
95 p2 = strrep (p2, "\\", "/"); |
|
96 endif |
6398
|
97 |
6679
|
98 ## Move the file(s). |
|
99 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, f1, f2)); |
|
100 if (err < 0) |
|
101 status = false; |
|
102 msgid = "movefile"; |
|
103 endif |
|
104 endwhile |
|
105 else |
|
106 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe"))) |
|
107 p1 = strrep (p1, "\\", "/"); |
|
108 p2 = strrep (p2, "\\", "/"); |
|
109 endif |
|
110 |
|
111 ## Move the file(s). |
|
112 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, f1, f2)); |
|
113 if (err < 0) |
|
114 status = false; |
|
115 msgid = "movefile"; |
|
116 endif |
6047
|
117 endif |
|
118 else |
|
119 print_usage (); |
|
120 endif |
|
121 endfunction |