Sunday, July 26, 2009

copying file

copyfile

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.

Syntax


*

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

Description

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 is 0 for no error. Only one output argument is required and the f input argument is optional.

Examples

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

*

copyfile('myfun.m','myfun2.m')

Copy File to Another Directory

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

*

copyfile('myfun.m','d:/work/myfiles')

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

*

copyfile('myfiles/my*','../newprojects')

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:

*

[s,mess,messid]=copyfile('myfiles','d:/work/myfiles')
s =
1

mess =
''

messid =
''

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:

*

copyfile('myfile.m','d:/work/restricted','f')

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

No comments:

Post a Comment