Mercurial > hg > octave-jordi
annotate scripts/miscellaneous/copyfile.m @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | 1bf0ce0930be |
children | 95c3e38098bf |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 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 | |
6645 | 43 if (ispc () && ! isunix () && isempty (file_in_path (EXEC_PATH, "cp.exe"))) |
6233 | 44 ## Windows. |
6210 | 45 cmd = "cmd /C xcopy /E"; |
46 cmd_force_flag = "/Y"; | |
47 else | |
48 cmd = "cp -r"; | |
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))) | |
55 error ("copyfile: first argument must be a character string or a cell array of character strings"); | |
56 endif | |
57 | |
58 if (! ischar (f2)) | |
59 error ("copyfile: second argument must be a character string"); | |
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 |
6679 | 70 |
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 ("copyfile: when copying multiple files, second argument must be a directory"); | |
75 endif | |
6233 | 76 |
77 ## Protect the file name(s). | |
78 f1 = glob (f1); | |
7352 | 79 if (isempty (f1)) |
80 error ("copyfile: no files to move"); | |
81 endif | |
6679 | 82 p1 = sprintf ("\"%s\" ", f1{:}); |
83 p2 = tilde_expand (f2); | |
84 | |
85 if (isdir && length(p1) > max_cmd_line) | |
86 l2 = length(p2) + length (cmd) + 6; | |
87 while (! isempty(f1)) | |
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 | |
95 | |
96 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe"))) | |
97 p1 = strrep (p1, "\\", "/"); | |
98 p2 = strrep (p2, "\\", "/"); | |
99 endif | |
6069 | 100 |
6679 | 101 ## Copy the files. |
102 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2)); | |
103 if (err < 0) | |
104 status = false; | |
105 msgid = "copyfile"; | |
106 break; | |
107 endif | |
108 endwhile | |
109 else | |
110 if (ispc () && ! isunix () && ! isempty (file_in_path (EXEC_PATH, "cp.exe"))) | |
111 p1 = strrep (p1, "\\", "/"); | |
112 p2 = strrep (p2, "\\", "/"); | |
113 endif | |
6398 | 114 |
6679 | 115 ## Copy the files. |
116 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2)); | |
117 if (err < 0) | |
118 status = false; | |
119 msgid = "copyfile"; | |
120 endif | |
6047 | 121 endif |
122 else | |
123 print_usage (); | |
124 endif | |
125 | |
126 endfunction |