MATLAB Function Reference Previous page   Next Page

Copy file or directory

Graphical Interface

As an alternative to the copyfile function, use the Current Directory browser. Select the files and then select copy and paste commands from the Edit menu.



copyfile('source','destination') copies the file or directory, source (and all its contents) to the file or directory, destination, where source and destination are the absolute or relative pathnames for the directory or file. If source is a directory, destination cannot be a file. If source is a directory, copyfile copies the contents of source, not the directory itself. To rename a file or directory when copying it, make destination a different name than source. If destination already exists, copyfile replaces it without warning. Use the wildcard * at the end of source to copy all matching files. Note that the read-only and archive attributes of source are not preserved in destination.

copyfile('source','destination','f') copies source to destination, regardless of the read-only attribute of destination.

[status,message,messageid] = copyfile('source','destination','f') copies source to destination, returning the status, a message, and the MATLAB error message ID (see error and lasterr). Here, status is 1 for success and 0 for error. Only one output argument is required and the f input argument is optional.

The * wildcard in a path string is supported. Current behavior of copyfile differs between UNIX and Windows when using the wildcard * or copying directories.


Copy File in Current Directory, Assigning a New Name to It

To make a copy of a file myfun.m in the current directory, assigning it the name myfun2.m, type

Copy File to Another Directory

To copy myfun.m to the directory d:/work/myfiles, keeping the same filename, type

Copy All Matching Files by Using a Wildcard

To copy all files in the directory myfiles whose names begin with my to the directory newprojects, where newprojects is at the same level as the current directory, type

Copy Directory and Return Status

In this example, all files and subdirectories in the current directory's myfiles directory are copied to the directory d:/work/myfiles. Note that before running the copyfile function, d:/work does not contain the directory myfiles. It is created because myfiles is appended to destination in the copyfile function:

The message returned indicates that copyfile was successful.

Copy File to Read-Only Directory

Copy myfile.m from the current directory to d:/work/restricted, where restricted is a read-only directory:

After the copy, myfile.m exists in d:/work/restricted.

See Also

cd, delete, dir, fileattrib, filebrowser, fileparts, mkdir, movefile, rmdir

Previous page  convn copyobj Next page

© 1994-2005 The MathWorks, Inc.