copy function

void copy(
  1. String targetPath,
  2. String destination
)

Implementation

void copy(String targetPath, String destination) {
  Directory targetDir = Directory(targetPath);
  try {
    print("Trying Copying directory: $targetPath");
    if (targetDir.existsSync()) {
      print("Target path exists, copying directory...");
      String targetBasename = pathlib.basename(targetPath);

      // Create target
      Directory newPath = Directory(pathlib.join(destination, targetBasename))
        ..create();
      for (var fileOrDir in targetDir.listSync()) {
        // if it was file
        if (fileOrDir is File) {
          print("Copying file: ${fileOrDir.path} to ${newPath.path}\n");
          fileOrDir.copy(
              pathlib.join(newPath.path, pathlib.basename(fileOrDir.path)));
        } else if (fileOrDir is Directory) {
          print("Copying directory: ${fileOrDir.path} to ${newPath.path}\n");
          // recursion
          copy(fileOrDir.path, newPath.path);
        }
        // if ? is link then ...
        else {
          fileOrDir.rename(
              pathlib.join(newPath.path, pathlib.basename(fileOrDir.path)));
        }
      }
    } else {
      throw FileSystemException("Target does not exist", targetPath);
    }
  } catch (e) {
    rethrow;
  }
}