Main Content

copyfile

Copy file or folder

Description

copyfile source copies the file or folder source to the current folder. After a successful copyfile operation, the timestamp for the new file is the same as the timestamp for source.

copyfile source destination copies source to the file or folder destination.

  • If source is a file, then destination can be a file or folder.

  • If source is a folder, then destination must be a folder.

  • If source is a folder or specifies multiple files and destination does not exist, then copyfile attempts to create destination.

example

copyfile source destination f copies source to destination, even when destination is not writable. The state of the read/write attribute for destination does not change.

example

copyfile(source,destination,CopyLinkBehavior=slbehavior) specifies whether to copy a symbolic link or its target. (since R2024b)

status = copyfile(___) copies the specified file or folder and returns a status of 1 if the operation is successful. Otherwise, copyfile returns 0. You can use this syntax with any of the input argument combinations in the previous syntaxes.

example

[status,msg] = copyfile(___) also returns the message text for any warning or error that occurs.

example

[status,msg,msgID] = copyfile(___) also returns the message ID for any warning or error that occurs.

example

Examples

collapse all

Copy myfile1.m from the current folder to the subfolder myFolder.

mkdir myFolder
copyfile myfile1.m myFolder

Create a copy of myfile1.m in the current folder, assigning it the name myfile2.m.

copyfile myfile1.m myfile2.m

Copy files and subfolders with names beginning with my from the current folder to the folder newFolder, where newFolder does not already exist.

copyfile my* newFolder

Copy 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

Copy and rename the file myfile1.m. A status of 0 shows the copy was unsuccessful.

status = copyfile('myfile1.m', 'restricted');
status
status = logical
   0

Copy 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] = copyfile('myfile1.m', 'restricted', 'f');
status
status = logical
   1

message
message =

  0x0 empty char array
messageId
messageId =

  0x0 empty char array

Input Arguments

collapse all

File or folder to copy, specified as a string scalar or character vector. To copy multiple files or folders, use wildcards (*). When copying multiple files or folders, copyfile copies only the files and folders that can be copied.

source can be an absolute or relative path when copying local files or folders. However, for files and folders at a remote location, you must specify the full path as a uniform resource locator (URL). Internet URLs must include the protocol type "http://" or "https://". For more information, see Work with Remote Data.

Note

If source is a string, enclose all the inputs in parentheses. For example, copyfile("myfile.m","newfolder").

File or folder destination, specified as a string scalar or character vector. destination cannot include wildcards (*).

If destination is a local location, it can be specified as an absolute or relative path. If folders specified in destination do not exist, copyfile creates those folders. 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, copyfile("myfile.m","newfolder").

Since R2024b

Symbolic link behavior, specified as one of these values:

  • "osdefault" – Use the default behavior of the file system.

  • "preserve" – Copy the symbolic link and preserve it as a symbolic link at the destination. The target of the symbolic link remains the same.

  • "resolve" – Copy the target of the symbolic link.

Output Arguments

collapse all

Copy 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, ''.

Limitations

  • MATLAB® does not support internet URLs that require authentication.

  • MATLAB Online™ supports internet URLs associated with Microsoft® OneDrive™ files and folders, while the installed version of MATLAB supports only local OneDrive files.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced before R2006a

expand all