file copy

Copies files and folders.

file copy ?-force? ?--? SOURCE TARGET
file copy ?-force? ?--? SOURCE ?SOURCE ...? DIR

Description

Copies SOURCE to TARGET. When TARGET is a folder, the copy goes into it with the name of SOURCE, and with more than one SOURCE the last argument must be a folder. A folder copies with all its content.

A TARGET that exists is an error, unless -force is there. -- ends the options, for a SOURCE that starts with -.

Arguments

ArgumentTypeDefaultDescription
SOURCEpathrequiredA file or a folder to copy.
TARGETpathrequiredThe new path, or a folder for the copies.

Options

OptionTypeDefaultSummary
-forceflagnoneReplaces a target that exists.
--flagnoneEnds the options.

-force

Replaces a target that exists.

With -force a TARGET that exists is replaced by the copy.

% set f [open p.txt w]; puts -nonewline $f hello; close $f
> set f [open q.txt w]; close $f
> file copy -force p.txt q.txt
> file size q.txt
5
% file copy -bogus a b
bad option "-bogus": must be -force or --
-errorcode NONE

--

Ends the options.

The next argument is a SOURCE, also when it starts with -.

% set f [open -x w]; close $f
> file copy -- -x y
> file exists y
1

A flag has no value to check.

Returns

An empty string.

Examples

% set f [open n.txt w]; close $f
> file copy n.txt m.txt
> file exists m.txt
1

% set f [open a.txt w]; close $f
> file mkdir dd
> file copy a.txt dd
> file exists dd/a.txt
1

% file mkdir src/sub; set f [open src/sub/f w]; close $f
> file copy src dst
> file exists dst/sub/f
1

Errors

% file copy nope.txt x.txt
error copying "nope.txt": no such file or directory
-errorcode POSIX ENOENT {no such file or directory}

% set f [open n2.txt w]; close $f
> file copy n2.txt m2.txt
> file copy n2.txt m2.txt
error copying "n2.txt" to "m2.txt": target file exists
-errorcode POSIX EEXIST {file already exists}

% file copy
wrong # args: should be "file copy ?-option value ...? source ?source ...? target"
-errorcode NONE

Differences from Tcl

Facts

Capability
file. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also