Definition: | bool res = copy_file(string src, string dest, bool overwrite=false) |
Description: |
Copy a file.
src: the name of the file to copy. It must exist and cannot be a directory. dest: the new name or location of the file. overwrite: false (0/the default) prevents an existing destination file from being overwritten. true (non-zero) permits the overwriting of any existing destination file. Returns: 0 (false) on failure, 1 (true) on success. |
Comments: |
If overwrite is true, and if dest file already exists, the function overwrites the existing file and succeeds.
If dest is a directory, a file of the same name as source is created therein. Unlike some other routines here, any path separators should be appropriate for the operating system, namely backslash ('\\') on Windows and forwardslash ('/') on Linux. For best results, use fully qualified absolute pathnames. You should not rely on relative paths in dest any more than you do in src; for instance copy_file("demo\\test","demo\\test2") is fine, whereas copy_file("demo\\test","test2") is likely to copy to the parent directory, and copy_file("C:\\Program Files (x86)\\Phix\\demo\\test","test2") is likely to copy it to the current directory. The precise behaviour of path-less operations is operating system dependent. If overwrite was requested but the copy fails, there is no guarantee that any existing destination file is preserved, deleted, corrupted, or all three. The use of a named parameter when setting the overwrite parameter recommended, to make the intent clear and the code easier to read. |
Example: |
if not copy_file("blank.bmp",new_bmp,overwrite:=true) then fatal("cannot create "&newname) end if |
See Also: | move_file, rename_file |