11606
|
1 ## Copyright (C) 2005, 2006, 2007, 2008 John W. Eaton |
6047
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6047
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6047
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} movefile (@var{f1}, @var{f2}) |
|
21 ## Move 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. |
|
24 ## |
|
25 ## If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\ |
|
26 ## character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\ |
|
27 ## system-dependent error message, and @var{msgid} contains a unique\n\ |
|
28 ## message identifier.\n\ |
|
29 ## @seealso{glob} |
|
30 ## @end deftypefn |
|
31 |
|
32 function [status, msg, msgid] = movefile (f1, f2, force) |
|
33 |
6679
|
34 max_cmd_line = 1024; |
6047
|
35 status = true; |
|
36 msg = ""; |
|
37 msgid = ""; |
|
38 |
6210
|
39 ## FIXME -- maybe use the same method as in ls to allow users control |
|
40 ## over the command that is executed. |
|
41 |
6679
|
42 if (ispc () && ! isunix () && isempty (file_in_path (EXEC_PATH, "mv.exe"))) |
6233
|
43 ## Windows. |
6210
|
44 cmd = "cmd /C move"; |
|
45 cmd_force_flag = "/Y"; |
|
46 else |
|
47 cmd = "mv"; |
|
48 cmd_force_flag = "-f"; |
|
49 endif |
|
50 |
6047
|
51 if (nargin == 2 || nargin == 3) |
6233
|
52 ## Input type check. |
|
53 if (! (ischar (f1) || iscellstr (f1))) |
6679
|
54 error ("movefile: first argument must be a character string or a cell array of character strings"); |
6233
|
55 endif |
|
56 |
|
57 if (! ischar (f2)) |
6679
|
58 error ("movefile: second argument must be a character string"); |
6233
|
59 endif |
|
60 |
6047
|
61 if (nargin == 3 && strcmp (force, "f")) |
6210
|
62 cmd = strcat (cmd, " ", cmd_force_flag); |
6047
|
63 endif |
6069
|
64 |
6233
|
65 ## If f1 isn't a cellstr convert it to one. |
|
66 if (ischar (f1)) |
|
67 f1 = cellstr (f1); |
6069
|
68 endif |
6233
|
69 |
6679
|
70 ## If f1 has more than 1 element f2 must be a directory |
|
71 isdir = (exist (f2, "dir") != 0); |
|
72 if (length(f1) > 1 && ! isdir) |
|
73 error ("movefile: when moving multiple files, second argument must be a directory"); |
|
74 endif |
|
75 |
6233
|
76 ## Protect the file name(s). |
|
77 f1 = glob (f1); |
11606
|
78 if (isempty (f1)) |
|
79 error ("movefile: no files to move"); |
|
80 endif |
6679
|
81 p1 = sprintf ("\"%s\" ", f1{:}); |
|
82 p2 = tilde_expand (f2); |
6069
|
83 |
6679
|
84 if (isdir && length(p1) > max_cmd_line) |
|
85 l2 = length(p2) + length (cmd) + 6; |
|
86 while (! isempty(f1)) |
|
87 p1 = sprintf ("\"%s\" ", f1{1}); |
|
88 f1(1) = []; |
|
89 while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 < |
|
90 max_cmd_line)) |
|
91 p1 = sprintf ("%s\"%s\" ", p1, f1{1}); |
|
92 f1(1) = []; |
|
93 endwhile |
|
94 |
|
95 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe"))) |
|
96 p1 = strrep (p1, "\\", "/"); |
|
97 p2 = strrep (p2, "\\", "/"); |
|
98 endif |
6398
|
99 |
6679
|
100 ## Move the file(s). |
6828
|
101 [err, msg] = system (sprintf ("%s %s \"%s\"", cmd, p1, p2)); |
6679
|
102 if (err < 0) |
|
103 status = false; |
|
104 msgid = "movefile"; |
|
105 endif |
|
106 endwhile |
|
107 else |
|
108 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe"))) |
|
109 p1 = strrep (p1, "\\", "/"); |
|
110 p2 = strrep (p2, "\\", "/"); |
|
111 endif |
|
112 |
|
113 ## Move the file(s). |
6828
|
114 [err, msg] = system (sprintf ("%s %s \"%s\"", cmd, p1, p2)); |
6679
|
115 if (err < 0) |
|
116 status = false; |
|
117 msgid = "movefile"; |
|
118 endif |
6047
|
119 endif |
|
120 else |
|
121 print_usage (); |
|
122 endif |
|
123 endfunction |