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 |
|
35 status = true; |
|
36 msg = ""; |
|
37 msgid = ""; |
|
38 |
|
39 if (nargin == 2 || nargin == 3) |
|
40 if (nargin == 3 && strcmp (force, "f")) |
|
41 cmd = "/bin/mv -f"; |
|
42 else |
|
43 cmd = "/bin/mv"; |
|
44 endif |
6069
|
45 |
|
46 ## Allow cell input and protect the file name(s). |
|
47 if (iscellstr (f1)) |
|
48 f1 = sprintf("\"%s\" ", f1{:}); |
|
49 else |
|
50 f1 = sprintf("\"%s\" ", f1); |
|
51 endif |
|
52 |
|
53 [err, msg] = system (sprintf ("%s %s \"%s\"", cmd, f1, f2)); |
6047
|
54 if (err < 0) |
|
55 status = false; |
|
56 msgid = "movefile"; |
|
57 endif |
|
58 else |
|
59 print_usage (); |
|
60 endif |
|
61 |
|
62 ### status = true; |
|
63 ### msg = ""; |
|
64 ### msgid = "movefile"; |
|
65 ### |
|
66 ### if (nargin == 2) |
|
67 ### |
|
68 ### flist = glob (f1); |
|
69 ### nfiles = numel (flist); |
|
70 ### if (nfiles > 1) |
|
71 ### [f2info, err, msg] = stat (f2); |
|
72 ### if (err < 0) |
|
73 ### status = false; |
|
74 ### else |
|
75 ### if (S_ISDIR (f2info.mode)) |
|
76 ### for i = 1:nfiles |
|
77 ### [err, msg] = rename (flist{i}, f2); |
|
78 ### if (err < 0) |
|
79 ### status = false; |
|
80 ### break; |
|
81 ### endif |
|
82 ### endfor |
|
83 ### else |
|
84 ### status = false; |
|
85 ### msg = "when moving multiple files, destination must be a directory"; |
|
86 ### endif |
|
87 ### endif |
|
88 ### else |
|
89 ### [err, msg] = rename (f1, f2); |
|
90 ### if (err < 0) |
|
91 ### status = false; |
|
92 ### break; |
|
93 ### endif |
|
94 ### endif |
|
95 ### |
|
96 ### else |
|
97 ### usage ("movefile (f1, f2)"); |
|
98 ### endif |
|
99 ### |
|
100 ### if (status) |
|
101 ### msgid = ""; |
|
102 ### endif |
|
103 |
|
104 endfunction |