FileCopy - Syntax & Usage | AutoHotkey

AutoHotkey

FileCopy

Copies one or more files.

FileCopy, SourcePattern, DestPattern , Flag

Parameters

SourcePattern

The name of a single file or folder, or a wildcard pattern such as C:\Temp\*.tmp. SourcePattern is assumed to be in %A_WorkingDir% if an absolute path isn't specified.

DestPattern
The name or pattern of the destination, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified. To perform a simple copy -- retaining the existing file name(s) -- specify only the folder name as shown in these functionally identical examples:
FileCopy, C:\*.txt, C:\My Folder
FileCopy, C:\*.txt, C:\My Folder\*.*
Flag

(optional) this flag determines whether to overwrite files if they already exist:

0 = (default) do not overwrite existing files
1 = overwrite existing files

This parameter can be an expression, even one that evalutes to true or false (since true and false are stored internally as 1 and 0).

ErrorLevel

[v1.1.04+]: This command is able to throw an exception on failure. For more information, see Runtime Errors.

ErrorLevel is set to the number of files that could not be copied due to an error, or 0 otherwise.

In either case, if the source file is a single file (no wildcards) and it does not exist, ErrorLevel is set to 0. To detect this condition, use IfExist or FileExist() on the source file prior to copying it.

Unlike FileMove, copying a file onto itself is always counted as an error, even if the overwrite mode is in effect.

If files were found, A_LastError is set to 0 (zero) or the result of the operating system's GetLastError() function immediately after the last failure. Otherwise A_LastError contains an error code that might indicate why no files were found.

Remarks

FileCopy copies files only. To instead copy the contents of a folder (all its files and subfolders), see the examples section below. To copy a single folder (including its subfolders), use FileCopyDir.

The operation will continue even if error(s) are encountered.

Related

FileMove, FileCopyDir, FileMoveDir, FileDelete

Examples

FileCopy, C:\My Documents\List1.txt, D:\Main Backup\  ; Make a copy but keep the orig. file name.
FileCopy, C:\My File.txt, C:\My File New.txt  ; Copy a file into the same folder by providing a new name.
FileCopy, C:\Folder1\*.txt, D:\New Folder\*.bkp  ; Copy to new location and give new extension.
; The following example copies all files and folders inside a folder to a different folder:
ErrorCount := CopyFilesAndFolders("C:\My Folder\*.*", "D:\Folder to receive all files & folders")
if ErrorCount <> 0
    MsgBox %ErrorCount% files/folders could not be copied.

CopyFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = false)
; Copies all files and folders matching SourcePattern into the folder named DestinationFolder and
; returns the number of files/folders that could not be copied.
{
    ; First copy all the files (but not the folders):
    FileCopy, %SourcePattern%, %DestinationFolder%, %DoOverwrite%
    ErrorCount := ErrorLevel
    ; Now copy all the folders:
    Loop, %SourcePattern%, 2  ; 2 means "retrieve folders only".
    {
        FileCopyDir, %A_LoopFileFullPath%, %DestinationFolder%\%A_LoopFileName%, %DoOverwrite%
        ErrorCount += ErrorLevel
        if ErrorLevel  ; Report each problem folder by name.
            MsgBox Could not copy %A_LoopFileFullPath% into %DestinationFolder%.
    }
    return ErrorCount
}