changelog shortlog tags changeset files revisions annotate raw

scripts/miscellaneous/copyfile.m

changeset 10289: 4b124317dc38
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (58 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2005, 2006, 2007, 2008, 2009 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 3 of the License, or (at
8## your option) 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, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
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}
22## may contain globbing patterns. If @var{f1} expands to multiple file
23## names, @var{f2} must be a directory. If @var{force} is given and equals
24## the string "f" the copy operation will be forced.
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, movefile}
31## @end deftypefn
32
33function [status, msg, msgid] = copyfile (f1, f2, force)
34
35 max_cmd_line = 1024;
36 status = true;
37 msg = "";
38 msgid = "";
39
40 ## FIXME -- maybe use the same method as in ls to allow users control
41 ## over the command that is executed.
42
43 if (ispc () && ! isunix () && isempty (file_in_path (EXEC_PATH, "cp.exe")))
44 ## Windows.
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
52 if (nargin == 2 || nargin == 3)
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
62 if (nargin == 3 && strcmp (force, "f"))
63 cmd = cstrcat (cmd, " ", cmd_force_flag);
64 endif
65
66 ## If f1 isn't a cellstr convert it to one.
67 if (ischar (f1))
68 f1 = cellstr (f1);
69 endif
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
76
77 ## Protect the file name(s).
78 f1 = glob (f1);
79 if (isempty (f1))
80 error ("copyfile: no files to move");
81 endif
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
100
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
114
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
121 endif
122 else
123 print_usage ();
124 endif
125
126endfunction