Mercurial > hg > octave-lyh
annotate scripts/miscellaneous/movefile.m @ 11477:a02d00dd3d5f
expm.m: new tests
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 10 Jan 2011 14:50:33 -0500 |
parents | 4ecc7bc5bc83 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
7352 | 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 | |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
42 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
43 && isempty (file_in_path (getenv ("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")) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7352
diff
changeset
|
63 cmd = cstrcat (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); | |
7352 | 79 if (isempty (f1)) |
80 error ("movefile: no files to move"); | |
81 endif | |
6679 | 82 p1 = sprintf ("\"%s\" ", f1{:}); |
83 p2 = tilde_expand (f2); | |
6069 | 84 |
6679 | 85 if (isdir && length(p1) > max_cmd_line) |
86 l2 = length(p2) + length (cmd) + 6; | |
87 while (! isempty(f1)) | |
10549 | 88 p1 = sprintf ("\"%s\" ", f1{1}); |
89 f1(1) = []; | |
90 while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 < | |
91 max_cmd_line)) | |
92 p1 = sprintf ("%s\"%s\" ", p1, f1{1}); | |
93 f1(1) = []; | |
94 endwhile | |
6679 | 95 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
96 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
97 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
10549 | 98 p1 = strrep (p1, "\\", "/"); |
99 p2 = strrep (p2, "\\", "/"); | |
100 endif | |
6398 | 101 |
10549 | 102 ## Move the file(s). |
103 [err, msg] = system (sprintf ("%s %s \"%s\"", cmd, p1, p2)); | |
104 if (err < 0) | |
105 status = false; | |
106 msgid = "movefile"; | |
107 endif | |
6679 | 108 endwhile |
109 else | |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
110 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
111 && ! isempty (file_in_path (getenv_path ("PATH"), "cp.exe"))) |
10549 | 112 p1 = strrep (p1, "\\", "/"); |
113 p2 = strrep (p2, "\\", "/"); | |
6679 | 114 endif |
115 | |
116 ## Move the file(s). | |
6828 | 117 [err, msg] = system (sprintf ("%s %s \"%s\"", cmd, p1, p2)); |
6679 | 118 if (err < 0) |
10549 | 119 status = false; |
120 msgid = "movefile"; | |
6679 | 121 endif |
6047 | 122 endif |
123 else | |
124 print_usage (); | |
125 endif | |
126 endfunction |