Log In    |    Knowledge Base    |    4D Home
Tech Tip: File and folder .copyTo() overwrite error
Published On: June 26, 2023

The file and folder classes have a .copyTo() function. This function is also used with ZIP archives to "extract" the files, but copying the contents of the compressed package to another location. When using the function, the operations may be repeatedly performed. If so, an error may occur due to the expected behavior of the .copyTo() function generating an error if a file with the same name already exists.

There are multiple options to handle the error, some suggestions are listed below.

Preemptively Checking the destination first: The destination directory can be checked for files of the same name being copied over. Code can then be implemented to address any conflicts.

Reactively Implementing an error handler: An error handler method can be used to trap the error and manage the issue in the desired way.

Automatically Overwrite the conflicts: The easiest was would be to used the optional Longint parameter fk overwrite (4) to allow the function to automatically overwrite any conflicting files with the new ones.