Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/copyfile.m @ 12211:11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Improve docstrings for functions in System Utilities::File Utilities.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 29 Jan 2011 21:28:38 -0800 |
parents | c792872f8942 |
children | 72c96de7a403 |
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 -*- | |
12211
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}) |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} copyfile (@var{f1}, @var{f2}, 'f') |
6152 | 22 ## Copy the file @var{f1} to the new name @var{f2}. The name @var{f1} |
6047 | 23 ## may contain globbing patterns. If @var{f1} expands to multiple file |
12211
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
24 ## names, @var{f2} must be a directory. If the force flag 'f' is given then |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
25 ## existing destination files will be overwritten without prompting. |
6047 | 26 ## |
12211
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
27 ## If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
28 ## character strings. Otherwise, @var{status} is 0, @var{msg} contains a |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
29 ## system-dependent error message, and @var{msgid} contains a unique |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
30 ## message identifier. |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
31 ## @seealso{movefile} |
6047 | 32 ## @end deftypefn |
33 | |
34 function [status, msg, msgid] = copyfile (f1, f2, force) | |
35 | |
6679 | 36 max_cmd_line = 1024; |
6047 | 37 status = true; |
38 msg = ""; | |
39 msgid = ""; | |
40 | |
6210 | 41 ## FIXME -- maybe use the same method as in ls to allow users control |
42 ## over the command that is executed. | |
43 | |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
44 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
45 && isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
6233 | 46 ## Windows. |
6210 | 47 cmd = "cmd /C xcopy /E"; |
48 cmd_force_flag = "/Y"; | |
49 else | |
50 cmd = "cp -r"; | |
51 cmd_force_flag = "-f"; | |
52 endif | |
53 | |
6047 | 54 if (nargin == 2 || nargin == 3) |
6233 | 55 ## Input type check. |
56 if (! (ischar (f1) || iscellstr (f1))) | |
57 error ("copyfile: first argument must be a character string or a cell array of character strings"); | |
58 endif | |
59 | |
60 if (! ischar (f2)) | |
61 error ("copyfile: second argument must be a character string"); | |
62 endif | |
63 | |
6047 | 64 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
|
65 cmd = cstrcat (cmd, " ", cmd_force_flag); |
6047 | 66 endif |
6069 | 67 |
6233 | 68 ## If f1 isn't a cellstr convert it to one. |
69 if (ischar (f1)) | |
70 f1 = cellstr (f1); | |
6069 | 71 endif |
6679 | 72 |
73 ## If f1 has more than 1 element f2 must be a directory | |
74 isdir = (exist (f2, "dir") != 0); | |
75 if (length(f1) > 1 && ! isdir) | |
76 error ("copyfile: when copying multiple files, second argument must be a directory"); | |
77 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
78 |
6233 | 79 ## Protect the file name(s). |
80 f1 = glob (f1); | |
7352 | 81 if (isempty (f1)) |
82 error ("copyfile: no files to move"); | |
83 endif | |
6679 | 84 p1 = sprintf ("\"%s\" ", f1{:}); |
85 p2 = tilde_expand (f2); | |
86 | |
87 if (isdir && length(p1) > max_cmd_line) | |
88 l2 = length(p2) + length (cmd) + 6; | |
89 while (! isempty(f1)) | |
10549 | 90 p1 = sprintf ("\"%s\" ", f1{1}); |
91 f1(1) = []; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
92 while (!isempty (f1) && (length(p1) + length(f1{1}) + l2 < |
10549 | 93 max_cmd_line)) |
94 p1 = sprintf ("%s\"%s\" ", p1, f1{1}); | |
95 f1(1) = []; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
96 endwhile |
6679 | 97 |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
98 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
99 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
10549 | 100 p1 = strrep (p1, "\\", "/"); |
101 p2 = strrep (p2, "\\", "/"); | |
102 endif | |
6069 | 103 |
10549 | 104 ## Copy the files. |
105 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2)); | |
106 if (err < 0) | |
107 status = false; | |
108 msgid = "copyfile"; | |
109 break; | |
110 endif | |
6679 | 111 endwhile |
112 else | |
11300
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
113 if (ispc () && ! isunix () |
4ecc7bc5bc83
search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents:
10549
diff
changeset
|
114 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe"))) |
10549 | 115 p1 = strrep (p1, "\\", "/"); |
116 p2 = strrep (p2, "\\", "/"); | |
6679 | 117 endif |
6398 | 118 |
6679 | 119 ## Copy the files. |
120 [err, msg] = system (sprintf ("%s %s\"%s\"", cmd, p1, p2)); | |
121 if (err < 0) | |
10549 | 122 status = false; |
123 msgid = "copyfile"; | |
6679 | 124 endif |
6047 | 125 endif |
126 else | |
127 print_usage (); | |
128 endif | |
129 | |
130 endfunction |