Mercurial > hg > octave-lyh
annotate scripts/miscellaneous/copyfile.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | 11faa69c4eaa |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2005-2011 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 -*- | |
6152 | 20 ## @deftypefn {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}, @var{force}) |
21 ## Copy the file @var{f1} to the new name @var{f2}. The name @var{f1} | |
6047 | 22 ## may contain globbing patterns. If @var{f1} expands to multiple file |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7540
diff
changeset
|
23 ## names, @var{f2} must be a directory. If @var{force} is given and equals |
6152 | 24 ## the string "f" the copy operation will be forced. |
6047 | 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\ | |
6152 | 30 ## @seealso{glob, movefile} |
6047 | 31 ## @end deftypefn |
32 | |
33 function [status, msg, msgid] = copyfile (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 | |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
43 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
44 && isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
6233 | 45 ## Windows. |
6210 | 46 cmd = "cmd /C xcopy /E"; |
47 cmd_force_flag = "/Y"; | |
48 else | |
49 cmd = "cp -r"; | |
50 cmd_force_flag = "-f"; | |
51 endif | |
52 | |
6047 | 53 if (nargin == 2 || nargin == 3) |
6233 | 54 ## Input type check. |
55 if (! (ischar (f1) || iscellstr (f1))) | |
56 error ("copyfile: first argument must be a character string or a cell array of character strings"); | |
57 endif | |
58 | |
59 if (! ischar (f2)) | |
60 error ("copyfile: second argument must be a character string"); | |
61 endif | |
62 | |
6047 | 63 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
|
64 cmd = cstrcat (cmd, " ", cmd_force_flag); |
6047 | 65 endif |
6069 | 66 |
6233 | 67 ## If f1 isn't a cellstr convert it to one. |
68 if (ischar (f1)) | |
69 f1 = cellstr (f1); | |
6069 | 70 endif |
6679 | 71 |
72 ## If f1 has more than 1 element f2 must be a directory | |
73 isdir = (exist (f2, "dir") != 0); | |
74 if (length(f1) > 1 && ! isdir) | |
75 error ("copyfile: when copying multiple files, second argument must be a directory"); | |
76 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
77 |
6233 | 78 ## Protect the file name(s). |
79 f1 = glob (f1); | |
7352 | 80 if (isempty (f1)) |
81 error ("copyfile: no files to move"); | |
82 endif | |
6679 | 83 p1 = sprintf ("\"%s\" ", f1{:}); |
84 p2 = tilde_expand (f2); | |
85 | |
86 if (isdir && length(p1) > max_cmd_line) | |
87 l2 = length(p2) + length (cmd) + 6; | |
88 while (! isempty(f1)) | |
10549 | 89 p1 = sprintf ("\"%s\" ", f1{1}); |
90 f1(1) = []; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
91 while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 < |
10549 | 92 max_cmd_line)) |
93 p1 = sprintf ("%s\"%s\" ", p1, f1{1}); | |
94 f1(1) = []; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
95 endwhile |
6679 | 96 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
97 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
98 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
10549 | 99 p1 = strrep (p1, "\\", "/"); |
100 p2 = strrep (p2, "\\", "/"); | |
101 endif | |
6069 | 102 |
10549 | 103 ## Copy the files. |
104 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2)); | |
105 if (err < 0) | |
106 status = false; | |
107 msgid = "copyfile"; | |
108 break; | |
109 endif | |
6679 | 110 endwhile |
111 else | |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
112 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
113 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
10549 | 114 p1 = strrep (p1, "\\", "/"); |
115 p2 = strrep (p2, "\\", "/"); | |
6679 | 116 endif |
6398 | 117 |
6679 | 118 ## Copy the files. |
119 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2)); | |
120 if (err < 0) | |
10549 | 121 status = false; |
122 msgid = "copyfile"; | |
6679 | 123 endif |
6047 | 124 endif |
125 else | |
126 print_usage (); | |
127 endif | |
128 | |
129 endfunction |