| MATLAB® | ![]() |
As an alternative to the cd function, you can copy files and directories using the Current Directory browser.
copyfile('source','destination')
copyfile('source','destination','f')
[status,message,messageid] = copyfile('source','destination','f')
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 path names for the directory or file. If source is a directory, destination also must be a directory. 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. To copy multiple files or directories, you can use one or more wildcard characters * after the last file separator in source. You cannot use a wildcard character in destination. You can rename when copying only when source is a single file. 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 lasterror). Here, status is 1 for success and 0 for error. Only one output argument is required and the f input argument is optional.
You can use a * (wildcard) in a path string. Current behavior of copyfile differs between the UNIX[1] and Windows platforms when using the wildcard (*) or copying directories.
The timestamp given to the destination file is identical to that taken from the source file.
To make a copy of a file myfun.m in the current directory, assigning it the name myfun2.m, type
copyfile('myfun.m','myfun2.m')To copy myfun.m to the directory d:/work/myfiles, keeping the same filename, type
copyfile('myfun.m','d:/work/myfiles')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
copyfile('myfiles/my*','../newprojects')To copy all files and subdirectories in the current directory's myfiles directory to the directory d:/work/myfiles. :
[s,mess,messid]=copyfile('myfiles','d:/work/myfiles')
s =
1
mess =
''
messid =
''The message returned indicates that copyfile was successful.
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
Copy myfile.m from the current directory to d:/work/restricted, where restricted is a read-only directory:
copyfile('myfile.m','d:/work/restricted','f')After the copy, myfile.m exists in d:/work/restricted.
cd, delete, dir, fileattrib, filebrowser, fileparts, mkdir, movefile, rmdir
Managing Files and Working with the Current Directory
[1] UNIX is a registered trademark of The Open Group in the United States and other countries.
![]() | convn | copyobj | ![]() |
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |