CopyFile(src$, dst$[, t])
CopyFile(src$, dst$[, newname$, func, userdata, pattern$, matchdir])
src$
to the
directory specified in dst$
. Note that by default, existing files will be
overwritten without asking. You can customize this behaviour by specifying
a callback function (see below). Also note that you have to specify a directory,
not a file, in dst$
.
This function is powerful. It will fully recurse into all subdirectories
and copy the file attributes, date stamps and comments as well. If the
destination directory does not exist, it will be created for you (even
if it contains subdirectories that do not exist yet). All path
specifications can be local to the current directory or qualified. You
can also copy files to the current directory by specifying "" as dst$
.
CopyFile()
supports many optional arguments. Before Hollywood 9.0, those
had to be passed as optional parameters (see above). Since Hollywood 9.0,
however, it is recommended to use the new syntax, which has a single optional
table argument that can be used to pass one or more optional arguments to
CopyFile()
.
The following table fields are recognized by this function:
NewName:
src$
.
Pattern:
CopyFile()
will only copy the files that match the specified pattern. For example, passing
*.jpg
in Pattern
will only copy files that use the .jpg
file extension.
Of course, using a filter pattern makes only sense if you pass a directory in src$
.
Note that for historical reasons, the pattern specified in Pattern
will also be matched
against all subdirectories that are to be copied. If you don't want that, set the
MatchDir
table tag to False
(see below). The pattern specified in Pattern
must adhere
to the pattern rules as described in the documentation of the MatchPattern()
function. See MatchPattern for details. (V5.0)
MatchDir:
Pattern
should also be matched against subdirectories. If this is set to True
, CopyFile()
will only recurse into subdirectories that match the specified filter pattern. If it
is set to False
, CopyFile()
will recurse into all subdirectories. For compatibility
reasons, MatchDir
defaults to True
, but most of the time you will want to pass False
here because it usually does not make sense to match a file pattern against
a directory name. For example, it does not make sense to match the *.jpg
example
from above against directories as well. (V5.0)
BufferSize:
FailOnError:
CopyFile()
will fail when an error occurs. You can change this behaviour
by setting FailOnError
to False
. In that case, CopyFile()
won't fail on an error but
instead your callback function, if there is one, will be notified using the #COPYFILE_FAILED
message and your callback must tell CopyFile()
how to proceed (retry, continue, abort). See
below to learn how to set up a callback function for CopyFile()
. FailOnError
defaults
to True
. (V9.0)
Force:
True
, write- or delete-protected files will automatically be overwritten
without asking the callback function first. Note that if there is no callback function
and Force
is set to False
(the default), CopyFile()
will fail if it can't overwrite a
file because that file is write- or delete-protected. Defaults to False
. (V9.0)
Async:
True
, CopyFile()
will operate in asynchronous mode. This means that
it will return immediately, passing an asynchronous operation handle to you. You can then
use this asynchronous operation handle to finish the operation by repeatedly calling
ContinueAsyncOperation() until it returns True
. This is
very useful in case your script needs to do something else while the operation is in progress,
e.g. displaying a status animation or something similar. By putting CopyFile()
into asynchronous
mode, it is easily possible for your script to do something else while the operation is
being processed. See ContinueAsyncOperation for details. Defaults to False
. (V9.0)
Adapter:
UserTags:
DstAdapter:
default
. See Loaders and adapters for details. (V10.0)
DstUserTags:
DstAdapter
. See above for details. If you use this tag, you must set
it to a table of key-value pairs that contain the additional data that should be passed
to plugins. See User tags for details. (V10.0)
Callback:
CopyFile()
on various occasions, e.g. to allow you to update a progress bar for example. The callback
function also gets called if a destination file already exists or is write/delete
protected. The callback function receives one argument: A table that contains more information.
The following callback types are available:
#COPYFILE_OVERWRITE:
CopyFile()
will run this callback to ask if a file can be overwritten. Your callback function
has to return True
if the file shall be overwritten or False
if it shall be skipped. To
abort the copy operation completely, return -1. The following fields will be set in the
table parameter that is passed to your callback function:
Action:
#COPYFILE_OVERWRITE
Source:
Destination:
UserData:
UserData
table field (see below).
#COPYFILE_UNPROTECT:
#COPYFILE_UNPROTECT
will be called if
the file that should be overwritten is write- or delete-protected.
This callback function needs to return True
if it is okay to unprotect
the file or False
if it shall be skipped. If you return -1,
the copy operation will be completely aborted.
Starting with Hollywood 9.0, your callback function can also return -2 to
indicate that copying should still be attempted even though the file is write-
or delete-protected. This, however, will typically lead to an error because
write- or delete-protected files can't be overwritten without unprotecting them
first. However, if you return -2 you can catch the ensuing error in your #COPYFILE_FAILED
callback if you have set FailOnError
to False
(see above).
The following fields will be set in the table parameter that is passed to your callback function:
Action:
#COPYFILE_UNPROTECT
Destination:
UserData:
UserData
table field (see below).
#COPYFILE_STATUS:
#COPYFILE_STATUS
should normally return False
. If it returns True
, the copy operation will be aborted.
The following fields will be set in the table parameter that is passed to your
callback function:
Action:
#COPYFILE_STATUS
Source:
Destination:
Copied:
Filesize:
UserData:
UserData
table field (see below).
#COPYFILE_FAILED:
FailOnError
tag has been set to False
(see above).
In that case, the callback function of type #COPYFILE_FAILED
will be called whenever a
copy operation has failed. It has to return True
to abort the copy operation, False
to
continue even though an error has occurred or -1 to retry the copy operation that has
just failed. The following fields will be set in the table parameter that is passed to your
callback function:
Action:
#COPYFILE_FAILED
Source:
Destination:
UserData:
UserData
table field (see below).
(V9.0)
UserData:
UserData
tag you can easily pass data to your callback function.
You can specify a value of any type in UserData
. Numbers, strings, tables, and
even functions can be passed as user data. Your callback will receive this
data in the UserData
field in the table that is passed to it. (V5.1)
CopyFile("image.png", "TestDir")Copy the file "image.png" from the current directory to "TestDir".
CopyFile("Images", "Images_Bak")Create a backup of the "Images" directory in the "Images_Bak" directory (the new directory will be created by this function automatically). All files including subdirectories will be copied to the new location.
CopyFile("Hollywood_Sources/WarpOS", "HW_BAK", {Pattern = "*.c;*.h", MatchDir = False})Copies all source code and header files from Hollywood_Sources/WarpOS to HW_BAK.
Function p_CopyCallback(msg) Switch msg.action Case #COPYFILE_STATUS: DebugPrint("Now copying", FilePart(msg.source), "to", PathPart(msg.destination)) Case #COPYFILE_OVERWRITE: Return(SystemRequest("Hollywood", FilePart(msg.destination) .. " does already exist!\nDo you want me to overwrite it?", "Yes|No")) Case #COPYFILE_UNPROTECT: Return(SystemRequest("Hollywood", FilePart(msg.destination) .. " is write/delete protected!\nDo you want me to unprotect it?", "Yes|No")) EndSwitch Return(False) EndFunction CopyFile("Images", "Copy_of_Images", {Callback = p_CopyCallback})Demonstrates the use of a callback function.