annotate scripts/miscellaneous/movefile.m @ 31706:597f3ee61a48 stable

update Octave Project Developers copyright for the new year
author John W. Eaton <jwe@octave.org>
date Fri, 06 Jan 2023 13:11:27 -0500
parents 5d3faba0342e
children 2e484f9f1f18
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
31706
597f3ee61a48 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30875
diff changeset
3 ## Copyright (C) 2005-2023 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
7 ##
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
14 ##
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
19 ##
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6828
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
25
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
25040
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
27 ## @deftypefn {} {} movefile @var{f1}
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
28 ## @deftypefnx {} {} movefile @var{f1} @var{f2}
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
29 ## @deftypefnx {} {} movefile @var{f1} @var{f2} f
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
30 ## @deftypefnx {} {} movefile (@var{f1})
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20713
diff changeset
31 ## @deftypefnx {} {} movefile (@var{f1}, @var{f2})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20713
diff changeset
32 ## @deftypefnx {} {} movefile (@var{f1}, @var{f2}, 'f')
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
33 ## @deftypefnx {} {[@var{status}] =} movefile (@dots{})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
34 ## @deftypefnx {} {[@var{status}, @var{msg}] =} movefile (@dots{})
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20713
diff changeset
35 ## @deftypefnx {} {[@var{status}, @var{msg}, @var{msgid}] =} movefile (@dots{})
25040
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
36 ## Move the source file or directory @var{f1} to the destination @var{f2}.
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
37 ##
25040
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
38 ## The name @var{f1} may contain globbing patterns, or may be a cell array of
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
39 ## strings. If @var{f1} expands to multiple filenames, @var{f2} must be a
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
40 ## directory.
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
41 ##
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
42 ## If no destination @var{f2} is specified then the destination is the present
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
43 ## working directory. If @var{f2} is a filename then @var{f1} is renamed to
328518881291 Add command forms to documentation of copyfile, mkdir, movefile.
Rik <rik@octave.org>
parents: 24534
diff changeset
44 ## @var{f2}.
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
45 ##
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
46 ## When the force flag @qcode{'f'} is given any existing files will be
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
47 ## overwritten without prompting.
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
48 ##
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
49 ## If successful, @var{status} is logical 1, and @var{msg}, @var{msgid} are
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
50 ## empty character strings (""). Otherwise, @var{status} is logical 0,
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
51 ## @var{msg} contains a system-dependent error message, and @var{msgid}
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
52 ## contains a unique message identifier. Note that the status code is exactly
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
53 ## opposite that of the @code{system} command.
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17394
diff changeset
54 ## @seealso{rename, copyfile, unlink, delete, glob}
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
55 ## @end deftypefn
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
56
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
57 function [status, msg, msgid] = movefile (f1, f2, force)
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
58
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 28104
diff changeset
59 if (nargin < 1)
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
60 print_usage ();
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
61 endif
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
62
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6398
diff changeset
63 max_cmd_line = 1024;
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
64 sts = true;
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
65 msg = "";
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
66 msgid = "";
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
67
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
68 ## FIXME: maybe use the same method as in ls to allow users control
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
69 ## over the command that is executed.
6210
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
70
11300
4ecc7bc5bc83 search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
71 if (ispc () && ! isunix ()
4ecc7bc5bc83 search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
72 && isempty (file_in_path (getenv ("PATH"), "mv.exe")))
6233
a299c8a6d96e [project @ 2007-01-09 04:31:18 by jwe]
jwe
parents: 6210
diff changeset
73 ## Windows.
6210
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
74 cmd = "cmd /C move";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
75 cmd_force_flag = "/Y";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
76 else
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
77 cmd = "mv";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
78 cmd_force_flag = "-f";
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
79 endif
12b676a0b183 [project @ 2006-12-07 02:37:17 by jwe]
jwe
parents: 6069
diff changeset
80
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
81 ## Input type check.
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
82 if (ischar (f1))
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
83 f1 = cellstr (f1);
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
84 elseif (! iscellstr (f1))
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
85 error ("copyfile: F1 must be a string or a cell array of strings");
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
86 endif
6233
a299c8a6d96e [project @ 2007-01-09 04:31:18 by jwe]
jwe
parents: 6210
diff changeset
87
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
88 if (nargin == 1)
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
89 f2 = pwd ();
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
90 elseif (! ischar (f2))
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
91 error ("movefile: F2 must be a string");
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
92 endif
6069
67b1a61a85ce [project @ 2006-10-21 14:33:53 by jwe]
jwe
parents: 6047
diff changeset
93
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
94 if (nargin == 3 && strcmp (force, "f"))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
95 cmd = [cmd " " cmd_force_flag];
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
96 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
97
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
98 ## If f1 has more than 1 element f2 must be a directory
25781
e04c56bbbace isdir.m: Make m-file a legacy function (bug #54489)
Rik <rik@octave.org>
parents: 25054
diff changeset
99 isdir = isfolder (f2);
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
100 if (numel (f1) > 1 && ! isdir)
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
101 if (nargout == 0)
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
102 error ("movefile: when copying multiple files, F2 must be a directory");
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
103 else
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
104 status = false;
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
105 msg = "when copying multiple files, F2 must be a directory";
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
106 msgid = "movefile";
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
107 return;
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
108 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
109 endif
6069
67b1a61a85ce [project @ 2006-10-21 14:33:53 by jwe]
jwe
parents: 6047
diff changeset
110
20713
2469d78a1d8b Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents: 19697
diff changeset
111 ## Protect the filename(s).
29093
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
112 if (ispc ())
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
113 f1 = __wglob__ (f1);
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
114 else
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
115 f1 = glob (f1);
1c212b36a35e Use "__wglob__" instead of "glob" on Windows (bug #59287).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28896
diff changeset
116 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
117 if (isempty (f1))
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
118 if (nargout == 0)
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
119 error ("movefile: no files to move");
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
120 else
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
121 status = false;
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
122 msg = "no files to move";
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
123 msgid = "movefile";
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
124 return;
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
125 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
126 endif
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
127 p1 = sprintf ('"%s" ', f1{:});
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
128 p2 = tilde_expand (f2);
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
129
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
130 if (isdir && length (p1) > max_cmd_line)
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
131 l2 = length (p2) + length (cmd) + 6;
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
132 while (! isempty (f1))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
133 p1 = sprintf ('"%s" ', f1{1});
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
134 f1(1) = [];
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
135 while (! isempty (f1)
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
136 && (length (p1) + length (f1{1}) + l2 < max_cmd_line))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
137 p1 = sprintf ('%s"%s" ', p1, f1{1});
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 7540
diff changeset
138 f1(1) = [];
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
139 endwhile
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6398
diff changeset
140
11300
4ecc7bc5bc83 search PATH from environment for programs, not EXEC_PATH
John W. Eaton <jwe@octave.org>
parents: 10549
diff changeset
141 if (ispc () && ! isunix ()
12497
1536ed546219 Fix bug #32443 preventing 'pkg install' on Windows platforms.
Rik <octave@nomad.inbox5.com>
parents: 12211
diff changeset
142 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
143 p1 = strrep (p1, '\', '/');
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
144 p2 = strrep (p2, '\', '/');
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6398
diff changeset
145 endif
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6398
diff changeset
146
25790
872111558bc0 reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents: 25781
diff changeset
147 ## Close old file(s) in editor
27263
99aa1bcb8848 rename octave_link and octave_link_events classes, move inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
148 __event_manager_file_remove__ (p1, p2);
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6398
diff changeset
149 ## Move the file(s).
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
150 [err, msg] = system (sprintf ('%s %s "%s"', cmd, p1, p2));
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
151 if (err != 0)
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
152 sts = false;
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 7540
diff changeset
153 msgid = "movefile";
6679
a40b4060efff [project @ 2007-05-31 20:07:23 by dbateman]
dbateman
parents: 6398
diff changeset
154 endif
25790
872111558bc0 reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents: 25781
diff changeset
155 ## Load new file(s) in editor
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
156 __event_manager_file_renamed__ (sts);
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
157 endwhile
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
158 else
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
159 if (ispc () && ! isunix ()
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
160 && ! isempty (file_in_path (getenv ("PATH"), "cp.exe")))
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
161 p1 = strrep (p1, '\', '/');
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
162 p2 = strrep (p2, '\', '/');
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
163 endif
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
164
25790
872111558bc0 reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents: 25781
diff changeset
165 ## Close old file(s) in editor
27263
99aa1bcb8848 rename octave_link and octave_link_events classes, move inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
166 __event_manager_file_remove__ (p1, p2);
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
167 ## Move the file(s).
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
168 [err, msg] = system (sprintf ('%s %s "%s"', cmd, p1, p2));
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
169 if (err != 0)
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
170 sts = false;
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
171 msgid = "movefile";
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
172 endif
25790
872111558bc0 reload editor files when their path changes by using movefile (bug #43922)
Torsten <mttl@mailbox.org>
parents: 25781
diff changeset
173 ## Load new file(s) in editor
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
174 __event_manager_file_renamed__ (sts);
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
175 endif
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
176
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
177 if (nargout == 0)
30241
ba4aebad10d1 Return logical status variable from file functions for Matlab compatibility.
Rik <rik@octave.org>
parents: 29359
diff changeset
178 if (! sts)
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
179 error ("movefile: operation failed: %s", msg);
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
180 endif
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
181 else
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
182 status = sts;
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
183 endif
17394
b6867a09d7cf Return correct status code for copyfile, movefile.
Rik <rik@octave.org>
parents: 17338
diff changeset
184
6047
176f1c58a474 [project @ 2006-10-10 19:13:49 by jwe]
jwe
parents:
diff changeset
185 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
186
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
187
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
188 %!test
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
189 %! unwind_protect
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
190 %! f1 = tempname ();
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
191 %! tmp_var = pi;
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
192 %! save (f1, "tmp_var");
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
193 %! fid = fopen (f1, "rb");
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
194 %! assert (fid >= 0);
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
195 %! orig_data = fread (fid);
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
196 %! fclose (fid);
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 28789
diff changeset
197 %! f2 = tempname ();
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
198 %! assert (movefile (f1, f2));
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19261
diff changeset
199 %! assert (! exist (f1, "file"));
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19261
diff changeset
200 %! assert (exist (f2, "file"));
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
201 %! fid = fopen (f2, "rb");
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
202 %! assert (fid >= 0);
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
203 %! new_data = fread (fid);
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
204 %! fclose (fid);
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
205 %! if (orig_data != new_data)
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
206 %! error ("moved file not equal to original file!");
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
207 %! endif
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
208 %! unwind_protect_cleanup
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
209 %! delete (f2);
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
210 %! end_unwind_protect
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
211
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
212 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28892
diff changeset
213 %!error <Invalid call> movefile ()
19261
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
214 %!error <F1 must be a string> movefile (1, "foobar")
30a9df3770aa movefile.m: Overhaul function.
Rik <rik@octave.org>
parents: 17744
diff changeset
215 %!error <F2 must be a string> movefile ("foobar", 1)
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19261
diff changeset
216 %!error <F2 must be a directory> movefile ({"a", "b"}, "%_NOT_A_DIR_%")
28104
b2e0a2ddfd7d movefile.m: throw error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents: 27923
diff changeset
217 %!error <no files to move> movefile ("%_NOT_A_FILENAME1_%", "%_NOT_A_FILENAME2_%")