Function Reference


FileCopy

Copies one or more files.

FileCopy ( "source", "dest" [, flag = 0] )

Parameters

source The source path of the file(s) to copy. (* and ? wildcards accepted - See Remarks)
dest The destination path of the copied file(s).
flag [optional] this flag determines whether to overwrite files if they already exist.
Can be a combination of the following:
    $FC_NOOVERWRITE (0) = (default) do not overwrite existing files
    $FC_OVERWRITE (1) = overwrite existing files
    $FC_CREATEPATH (8) = Create destination directory structure if it doesn't exist (See Remarks).
Constants are defined in FileConstants.au3

Return Value

Success: 1.
Failure: 0.

Remarks

See FileFindFirstFile() for a discussion about wildcards.

The destination directory must already exist, except using with flag value $FC_CREATEPATH (8).
For instance the combined flag $FC_OVERWRITE (1) + $FC_CREATEPATH (8) overwrites the target file and pre-checks for the destination directory structure and if it doesn't exist creates it automatically.

Some file attributes can make the overwriting impossible, if this is the case look at FileSetAttrib() to change the attributes of a file.

Related

DirCopy(), DirCreate(), FileDelete(), FileMove()

Example

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = @TempDir & "\FileCopy.au3"

    ; Create a temporary file to copy.
    If Not FileCreate($sFilePath, "This is an example of using FileCopy.") Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")

    ; Copy Au3 files in the temporary directory to a new folder/directory called Au3Files.
    FileCopy(@TempDir & "\*.au3", @TempDir & "\Au3Files\", $FC_OVERWRITE + $FC_CREATEPATH)

    ; Display the temporary directory.
    ShellExecute(@TempDir)
EndFunc   ;==>Example

; Create a file.
Func FileCreate($sFilePath, $sString)
    Local $bReturn = True ; Create a variable to store a boolean value.
    If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False.
    Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite.
EndFunc   ;==>FileCreate