DirCopy

Copies a folder along with all its sub-folders and files (similar to xcopy) or the entire contents of an archive file such as ZIP.

DirCopy Source, Dest , Overwrite

Parameters

Source

Type: String

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"

Source can also be the path of an archive file, in which case its contents are extracted into the destination directory. The supported formats are .zip,.tar, .tar.gz and .tgz. Other formats, such as RAR and 7z, are not supported.

A plain .gz file is decompressed to a single file; see Dest.

Dest

Type: String

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"

Note: When Source is a plain .gz file, Dest is the path of the decompressed file. Any missing parent directories are created.

Overwrite

Type: Integer

If omitted, it defaults to 0. Otherwise, specify one of the following numbers to indicate whether to overwrite files if they already exist:

0: 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.

Other values are converted to Boolean as in an if expression.

Error Handling

An exception is thrown if an error occurs.

If the source directory contains any saved webpages consisting of a PageName.htm file and a corresponding directory named PageName_files, an exception may be thrown even when the copy is successful.

Remarks

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

On Linux and macOS, symbolic-link entries inside the source are copied as links, preserving their stored target paths. Their targets are not traversed. Merging into a symbolic-link entry in the destination raises an OSError.

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 DirMove to move that copy to the desired location.

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

DirMove, FileCopy, FileMove, FileDelete, file loops, DirSelect, SplitPath

Examples

Copies a directory to a new location.

DirCopy "C:\My Folder", "C:\Copy of My Folder"

Prompts the user to copy a folder.

SourceFolder := DirSelect(, 3, "Select the folder to copy")
if SourceFolder = ""
    return
; Otherwise, continue.
TargetFolder := DirSelect(, 3, "Select the folder IN WHICH to create the duplicate folder.")
if TargetFolder = ""
    return
; Otherwise, continue.
Result := MsgBox("A copy of the folder '" SourceFolder "' will be put into '" TargetFolder "'. Continue?",, 4)
if Result = "No"
    return
SplitPath SourceFolder, &SourceFolderName  ; Extract only the folder name from its full path.
try
    DirCopy SourceFolder, TargetFolder "\" SourceFolderName
catch
    MsgBox "The folder could not be copied, perhaps because a folder of that name already exists in '" TargetFolder "'."
return