FileCopyDir - Syntax & Usage | AutoHotkey

AutoHotkey

FileCopyDir

Copies a folder along with all its sub-folders and files (similar to xcopy).

FileCopyDir, Source, Dest , Flag

Parameters

Source

Name of the source directory (with no trailing backslash), which is assumed to be in %A_WorkingDir% if an absolute path isn't specified. For example: C:\My Folder

Dest

Name of the destination directory (with no trailing baskslash), which is assumed to be in %A_WorkingDir% if an absolute path isn't specified. For example: C:\Copy of My Folder

Flag

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

0 (default): Do not overwrite existing files. The operation will fail and have no effect if Dest already exists as a file or directory.

1: Overwrite existing files. However, any files or subfolders inside Dest that do not have a counterpart in Source will not be deleted.

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 1 if there was a problem or 0 otherwise. However, if the source directory contains any saved webpages consisting of a PageName.htm file and a corresponding directory named PageName_files, an error may be indicated even when the copy is successful.

Remarks

If the destination directory structure doesn't exist it will be created if possible.

Since the operation will recursively copy a folder along with all its subfolders and files, the result of copying a folder to a destination somewhere inside itself is undefined. To work around this, first copy it to a destination outside itself, then use FileMoveDir to move that copy to the desired location.

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

Related

FileMoveDir, FileCopy, FileMove, FileDelete, file-loops, FileSelectFolder, SplitPath

Examples

FileCopyDir, C:\My Folder, C:\Copy of My Folder

; Example #2: A working script that prompts you to copy a folder.
FileSelectFolder, SourceFolder, , 3, Select the folder to copy
if SourceFolder =
    return
; Otherwise, continue.
FileSelectFolder, TargetFolder, , 3, Select the folder IN WHICH to create the duplicate folder.
if TargetFolder =
    return
; Otherwise, continue.
MsgBox, 4, , A copy of the folder "%SourceFolder%" will be put into "%TargetFolder%".  Continue?
IfMsgBox, No
    return
SplitPath, SourceFolder, SourceFolderName  ; Extract only the folder name from its full path.
FileCopyDir, %SourceFolder%, %TargetFolder%\%SourceFolderName%
if ErrorLevel
    MsgBox The folder could not be copied, perhaps because a folder of that name already exists in "%TargetFolder%".
return