copyTree function
void
copyTree(})
Copy an entire directory tree from one location to another.
This function recursively copies all files and directories from from to to.
The destination directory must already exist.
Parameters:
from: Source directory path (must be a directory)to: Destination directory path (must exist and be a directory)overwrite: If true, overwrites existing files (defaults to false)filter: Function to filter which files to copy (defaults to copying all)includeHidden: If true, includes hidden files/directories (defaults to false)recursive: If true, copies subdirectories recursively (defaults to true)
Throws:
- Exception if source is not a directory
- Exception if destination doesn't exist or is not a directory
- Exception if any file copy operation fails
Example:
// Copy entire directory tree
copyTree('/path/to/source', '/path/to/destination');
// Copy with custom filter (only .txt files)
copyTree('/path/to/source', '/path/to/destination',
filter: (file) => file.endsWith('.txt'));
// Copy including hidden files
copyTree('/path/to/source', '/path/to/destination', includeHidden: true);
Implementation
void copyTree(
String from,
String to, {
bool overwrite = false,
bool Function(String file) filter = _allowAll,
bool includeHidden = false,
bool recursive = true,
}) {
// Validate input parameters
if (from.isEmpty) {
StatusHelper.failed('Source directory path cannot be empty.');
}
if (to.isEmpty) {
StatusHelper.failed('Destination directory path cannot be empty.');
}
final absoluteFrom = truepath(from);
final absoluteTo = truepath(to);
if (!isDirectory(absoluteFrom)) {
StatusHelper.failed('The source path $absoluteFrom must be a directory.');
}
if (!exists(absoluteTo)) {
StatusHelper.failed('The destination path $absoluteTo must already exist.');
}
if (!isDirectory(absoluteTo)) {
StatusHelper.failed(
'The destination path $absoluteTo must be a directory.');
}
try {
final items = find(
'*',
workingDirectory: absoluteFrom,
includeHidden: includeHidden,
recursive: recursive,
);
items.forEach((item) {
_process(
item,
filter,
absoluteFrom,
absoluteTo,
overwrite: overwrite,
recursive: recursive,
);
});
} on Exception catch (e) {
StatusHelper.failed(
'Failed to copy directory tree from $absoluteFrom to $absoluteTo: $e');
} catch (e) {
StatusHelper.failed(
'An unexpected error occurred copying directory tree from $absoluteFrom to $absoluteTo: $e');
}
}