File_Utilities module

copy_directory

procedure · 1 overloadSource

Copies the folder source and everything in it to dest, including empty sub-folders. Returns whether it all worked.

copy_directory :: (source: string, dest: string) -> bool

dest and any missing parents are created; an existing dest is merged into, and files with the same names are replaced. It refuses (false, nothing copied) when dest is source or inside it, since that would never end. Links are followed and copied as what they point to, with link cycles cut. As with copy_file, permissions and times are not copied. A failure part-way leaves what was copied so far.

Parameters

source: string
dest: string

Returns

bool
ExampleOpen in playground ↗
#import "Extensions/Environment";
#import "File";
base := environment_variable("TMPDIR");
if base == "" base = environment_variable("TEMP");
if base == "" base = "/tmp";
source := tprint("%/jai-doc-copy-tree", base);
dest := tprint("%/jai-doc-copy-tree-copy", base);
defer {
    file_delete(tprint("%/sub/leaf", source));
    delete_directory(tprint("%/sub", source));
    delete_directory(source);
    file_delete(tprint("%/sub/leaf", dest));
    delete_directory(tprint("%/sub", dest));
    delete_directory(dest);
}

assert(make_directory_if_it_does_not_exist(tprint("%/sub", source), recursive = true));
assert(write_entire_file(tprint("%/sub/leaf", source), "copper"));

assert(!copy_directory(source, tprint("%/inside", source))); // into itself
assert(copy_directory(source, dest));
text, ok := read_entire_file(tprint("%/sub/leaf", dest));
assert(ok && text == "copper");
free(text);

See also