Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

copyfile

Copy file or folder

Syntax

copyfile source
copyfile source destination
copyfile source destination f
status = copyfile(___)
[status,msg] = copyfile(___)
[status,msg,msgID] = copyfile(___)

Description

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

example

copyfile source destination copies the file or folder source to the file or folder destination. If source is a file, destination can be a file or a folder. If source is a folder, destination must also be a folder. If source is a folder or specifies multiple files and destination does not exist, 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

status = copyfile(___) copies the specified file or folder and returns a status of 1 if the operation is successful. Otherwise, it 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. You can use this syntax with any of the input argument combinations in the previous syntaxes.

example

[status,msg,msgID] = copyfile(___) also returns the message ID for any warning or error that occurs. You can use this syntax with any of the input argument combinations in the previous syntaxes.

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 whose names begin with my, from the current folder to the folder newFolder, where newFolder does not exist.

copyfile my* newFolder

Copy the file myfile1.m from the current folder to the read-only folder restricted and rename it to myfile2.m.

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, and message describes the issue.

[status,message,messageId] = copyfile('myfile1.m', 'restricted\myfile2.m')
status = logical
   0

message = 
'cp: cannot stat '/tmp/Bdoc17a_538369_144755/tp3899858f/myfile1.m': No such file or directory'
messageId = 
'MATLAB:COPYFILE:OSError'

Copy and rename the file myfile1.m using the 'f' option to override. A status of 1 and an empty message and messageID confirms the copy was successful.

[status,message,messageId] = copyfile('myfile1.m', 'restricted\myfile2.m', 'f')
status = logical
   0

message = 
'cp: cannot stat '/tmp/Bdoc17a_538369_144755/tp3899858f/myfile1.m': No such file or directory'
messageId = 
'MATLAB:COPYFILE:OSError'

Input Arguments

collapse all

File or folder name to copy, specified as a character vector. source can be an absolute or relative path. To copy multiple files or folders, use wildcards (*).

File or folder name to which to copy, specified as a character vector. destination can be an absolute or relative path, but cannot include wildcards.

Output Arguments

collapse all

Indication of whether the attempt to copy the file or folder is successful, specified as 0 or 1. If the attempt is successful, status is 1. Otherwise, status is 0.

Data Types: logical

Error message, specified 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, specified 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

  • After a successful copyfile operation, the timestamp for destination is the same as the timestamp for source.

Introduced before R2006a

Was this topic helpful?