movefile - Move or rename file or folder - MATLAB (original) (raw)
Move or rename file or folder
Syntax
Description
movefile [source](#f7-605782-source)
moves the file or folder source
to the current folder.movefile
does not preserve the archive attribute ofsource
.
movefile [source](#f7-605782-source) [destination](#f7-605782-destination)
moves source
to the file or folderdestination
. If source
anddestination
are in the same location, thenmovefile
renames source
todestination
. To rename a file or folder when moving it, make destination
a different name fromsource
and specify only one file or folder forsource
.
If source
is a folder, thendestination
must be a folder. Ifsource
is a folder or is capable of specifying multiple files and destination
does not exist, thenmovefile
createsdestination
.
movefile [source](#f7-605782-source) [destination](#f7-605782-destination) f
performs the move, even when destination
is not writable. The state of the read/write attribute for destination
does not change. This syntax overwrites read-only files.
movefile([source](#f7-605782-source),[destination](#f7-605782-destination),MoveLinkBehavior=[slbehavior](#mw%5F523d94c7-0fa7-428b-b594-6db77adc608e))
specifies whether to move a symbolic link or its target. (since R2024b)
[status](#f7-605782-status) = movefile(___)
moves the specified file or folder and returns a status of 1
if the operation is successful. Otherwise, movefile
returns0
. You can use this syntax with any of the input argument combinations in the previous syntaxes.
[[status](#f7-605782-status),[msg](#f7-605782-msg)] = movefile(___)
also returns the message text for any warning or error that occurs.
[[status](#f7-605782-status),[msg](#f7-605782-msg),[msgID](#f7-605782-msgID)] = movefile(___)
also returns the message ID for any warning or error that occurs.
Examples
Move files and folders to the current folder by omitting the destination input.
Create two folders: the first, myfiles
, containing the file myfile1.m
, and the second, myotherfiles
, containing the file myfile2.m
.
mkdir myfiles movefile myfile1.m myfiles mkdir myotherfiles movefile myfile2.m myotherfiles
Move myfile1.m
to the current folder. Since a destination is not specified, MATLAB® assumes the destination is the current folder.
movefile myfiles/myfile1.m
Set the current folder to myfiles
. Move myotherfiles
and its contents to the current folder.
cd myfiles movefile ../myotherfiles
Move files and subfolders whose names begin with my
from the current folder to the folder newFolder
, where newFolder
previously does not exist.
Create the folder myoldfolder
, and then rename it to mynewfolder
.
mkdir myoldfolder movefile myoldfolder mynewfolder
Move the file myfile1.m
from the current folder to the read-only folder restricted
.
Create the read-only folder restricted
.
mkdir restricted fileattrib restricted -w
Move the file myfile1.m
. A status of 0 shows the copy was unsuccessful.
status = movefile('myfile1.m','restricted'); status
Move the file myfile1.m
using the 'f' option to override the read-only status of the destination folder. A status of 1 and an empty message
and messageID
confirm the copy was successful.
[status,message,messageId] = movefile('myfile1.m','restricted','f'); status
message =
0×0 empty char array
messageId =
0×0 empty char array
Input Arguments
File or folder to move, specified as a character vector or string scalar. To move multiple files or folders, use wildcards (*). When moving multiple files or folders, movefile
moves only the files and folders that can be moved.
source
can be an absolute or relative path when moving local files or folders. However, to move files and folders at a remote location, source
must contain a full path specified as a uniform resource locator (URL). For more information, see Work with Remote Data.
Note
If source
is a string, enclose all the inputs in parentheses. For example,movefile("myfile.m","newfolder")
.
File or folder destination, specified as a character vector or string scalar. destination
cannot include wildcards (*).
If destination
is local, it can be specified as an absolute or relative path. If destination
is remote, it must contain a full path specified as a URL. For more information, see Work with Remote Data.
Note
If destination
is a string, enclose all the inputs in parentheses. For example,movefile("myfile.m","newfolder")
.
Since R2024b
Symbolic link behavior, specified as one of these values:
"preserve"
– Move the symbolic link and preserve it as a symbolic link at the destination. The target of the symbolic link remains the same."resolve"
– Move the target of the symbolic link.
Output Arguments
Move status, indicating if the attempt to move the file or folder is successful, returned as 0
or 1
. If the attempt is successful, the value of status
is 1. Otherwise, the value is 0.
Data Types: logical
Error message, returned as a character vector. If an error or warning occurs,msg
contains the message text of the error or warning. Otherwise, msg
is empty,''
.
Error message identifier, returned as a character vector. If an error or warning occurs,msgID
contains the message identifier of the error or warning. Otherwise, msgID
is empty,''
.
Tips
- This function supports Microsoft® Windows® shortcut files (
.lnk
).
Extended Capabilities
Usage notes and limitations:
- The
movefile
function can move files only to an empty directory, not to one that contains files. - The
movefile
function does not support wildcard operations or URLs as input. - Code generated from MATLAB® might behave differently from its execution within MATLAB.
In such instances, file handling in the generated code is similar to theMoveFileExA
function on Windows platforms or therename
function on UNIX® systems in theC
language.
Version History
Introduced before R2006a
You can move files and folders using symbolic links. When you specify a symbolic link as the source, you can control whether to move the symbolic link itself or move its target.
Starting in R2020a, on UNIX platforms, the wildcard expression *.*
no longer matches folders or files without an extension. In previous releases, the expression matches folders or files regardless of extension, including files without an extension. This change of behavior does not apply to Microsoft Windows platforms.