# Function Reference

# _WinAPI_CopyFileEx

Copies an existing file to a new file, notifying the application of its progress through a callback function

#include <WinAPIFiles.au3>
_WinAPI_CopyFileEx ( $sExistingFile, $sNewFile [, $iFlags = 0 [, $pProgressProc = 0 [, $pData = 0]]] )

## Parameters

$sExistingFile | The name of an existing file.
$sNewFile | The name of the new file.
$iFlags | [optional] The flags that specify how the file is to be copied. This parameter can be a combination of the following values: $COPY_FILE_ALLOW_DECRYPTED_DESTINATION (0x0008) $COPY_FILE_COPY_SYMLINK (0x0800) $COPY_FILE_FAIL_IF_EXISTS (0x0001) $COPY_FILE_NO_BUFFERING (0x1000) $COPY_FILE_OPEN_SOURCE_FOR_WRITE (0x0004) $COPY_FILE_RESTARTABLE (0x0002)
$pProgressProc | [optional] The address of a callback function that is called each time another portion of the file has been copied. (See MSDN for more information)
$pData | [optional] pointer to an argument to be passed to the callback function. Can be NULL.

## Return Value

Success: | True.
Failure: | False, call _WinAPI_GetLastError() to get extended error information

## Remarks

$COPY_* , $PROGRESS_* constants require #include <APIFilesConstants.au3>

## See Also

Search CopyFileEx in MSDN Library.

## Example

    #include <Misc.au3>
    #include <WinAPIError.au3>
    #include <WinAPIFiles.au3>

    Opt('TrayAutoPause', 0)

    Local $hProgressProc = DllCallbackRegister('_ProgressProc', 'bool', 'uint64;uint64;uint64;uint64;dword;dword;handle;handle;ptr')

    FileDelete(@TempDir & '\Test*.tmp')

    ProgressOn('_WinAPI_CopyFileEx()', 'Bigfile Creation...', '')
    Local $sFile = @TempDir & '\Test.tmp'
    Local $hFile = FileOpen($sFile, 2)
    For $i = 1 To 1000000
        FileWriteLine($hFile, "                                                     ")
    Next
    FileClose($hFile)

    ProgressOn('_WinAPI_CopyFileEx()', 'Copying...', '0%')

    If Not _WinAPI_CopyFileEx($sFile, @TempDir & '\Test1.tmp', 0, DllCallbackGetPtr($hProgressProc)) Then
        _WinAPI_ShowLastError('Error copying ' & $sFile)
    EndIf

    DllCallbackFree($hProgressProc)

    ProgressOff()

    FileDelete(@TempDir & '\Test*.tmp')

    Func _ProgressProc($iTotalFileSize, $iTotalBytesTransferred, $iStreamSize, $iStreamBytesTransferred, $iStreamNumber, $iCallbackReason, $hSourceFile, $hDestinationFile, $pData)
        #forceref $iStreamSize, $iStreamBytesTransferred, $iStreamNumber, $iCallbackReason, $hSourceFile, $hDestinationFile, $pData

        Local $iPercent = Round($iTotalBytesTransferred / $iTotalFileSize * 100)
        If $iPercent = 100 Then
            ProgressSet($iPercent, '', 'Complete')
        Else
            ProgressSet($iPercent, $iPercent & '%')
        EndIf
        Sleep(10) ; to slow down to see the progress bar

        If _IsPressed('1B') Then
            Return $PROGRESS_CANCEL
        Else
            Return $PROGRESS_CONTINUE
        EndIf
    EndFunc   ;==>_ProgressProc