Quantcast

Documentation Center

  • Trial Software
  • Product Updates

copyfile

Copy file or folder

Syntax

copyfile('source','destination')
copyfile('source','destination','f')
[status] = copyfile(___)
[status, message] = copyfile(___)
[status,message,messageid] = copyfile(___)

Description

copyfile('source','destination') copies the file or folder named source to the file or folder destination. The values for source and destination are 1 x n strings. Use full path names or path names relative to the current folder. To copy multiple files or folders, use one or more wildcard characters (*) after the last file separator in source. You cannot use a wildcard character in destination.

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. You can use f with any syntax for copyfile.

[status] = copyfile(___) reports the outcome as a logical scalar, status. The value is 1 for success and 0 for failure.

[status, message] = copyfile(___) returns any warning or error message as a string to message. When copyfile succeeds, message is an empty string.

[status,message,messageid] = copyfile(___) returns any warning or error identifier as a string to messageId. When copyfile succeeds, messageId is an empty string.

Examples

expand all

Copy File to Another Folder

Copy myFun.m from the current folder to d:/work/Projects/.

copyfile('myFun.m','d:/work/Projects/')

Copy File to Its Current Folder

Copy myFun.m in the current folder, assigning it the name myFun2.m.

copyfile('myFun.m','myFun2.m')

Copy Files and Folders to a New Folder Using Wildcards

Copy files and subfolders whose names begin with my, from the Projects subfolder within the current folder to the folder newProjects, which is at the same level as the current folder:

copyfile('Projects/my*','../newProjects/')

Copy Files to a New, Nonexistent Folder

Copy the contents of the Projects subfolder within the current folder to the I:/work/newProjects folder, where newProjects does not exist.

copyfile('Projects','I:/work/newProjects')

Copy File that Overwrites a Read-Only File

Copy the contents of myFun.m from the current folder to d:/work/restricted/myFun2.m, where myFun2.m is read-only.

[status,message,messageId]=copyfile('myFun.m', ...
'd:/work/restricted/myFun2.m','f')
status =
     1

message =
     ''

messageId =
     ''

The status of 1 and empty message and messageId strings confirm the copy was successful.

More About

expand all

Tips

  • The timestamp for destination is the same as the timestamp for source.

  • When source is a folder, destination must be a folder.

    • When source is a folder and destination does not exist, copyfile creates destination and copies the contents of source into destination.

    • When source is a folder and destination is an existing folder, copyfile copies the contents of source into destination.

    • When source is multiple files and destination does not exist, copyfile creates destination.

See Also

| | | | | | | |

Was this topic helpful?