-
-
Save thosakwe/681056e86673e73c4710cfbdfd2523a8 to your computer and use it in GitHub Desktop.
Recursively copy directory in Dart (requires "path")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* I'm sure there's a better way to do this, but this solution works for me. | |
* Recursively copies a directory + subdirectories into a target directory. | |
* There's also no error handling. Have fun. | |
*/ | |
import 'dart:io'; | |
import 'package:path/path.dart' as path; | |
Future<void> copyDirectory(Directory source, Directory destination) async { | |
await for (var entity in source.list(recursive: false)) { | |
if (entity is Directory) { | |
var newDirectory = | |
Directory(p.join(destination.absolute.path, p.basename(entity.path))); | |
await newDirectory.create(); | |
await copyDirectory(entity.absolute, newDirectory); | |
} else if (entity is File) { | |
await entity.copy(p.join(destination.path, p.basename(entity.path))); | |
} | |
} | |
} | |
// HOW TO USE IT: | |
await copyDirectory(Directory('cool_pics/tests'), Directory('new_pics/copy/new')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I will use it.
Thanks!