2014-11-29 3 views

Antwort

3

Nein, nicht meines Wissens gibt es nicht. Aber Dart unterstützt das grundlegende Lesen und Schreiben von Dateien aus Verzeichnissen. Es liegt nahe, dass dies programmatisch gelöst werden kann.

Auschecken this gist Ich fand ein Werkzeug, das diesen Prozess erreichen würde.

Grundsätzlich wäre suchen Sie das Verzeichnis für Dateien, die Sie den Kopiervorgang kopieren und ausführen wollte:

newFile.writeAsBytesSync(element.readAsBytesSync()); 

auf alle Dateipfade, new Path(element.path); im new Directory(newLocation);.

Edit:

Aber das ist super ineffizient, weil die ganzen Dateien vom System werden muss lesen und schrieben zurück in eine Datei. Sie könnten nur use a shell process von Dart gelaicht für Sie des Prozesses kümmern:

Process.run("cmd", ["/c", "copy", ...]) 
2

Sie James danken, eine schnelle Funktion für sie geschrieben, aber hat es einen alternativen Weg. Ich bin mir nicht sicher, ob dieser Weg effizienter wäre oder nicht?

/** 
* Retrieve all files within a directory 
*/ 
Future<List<File>> allDirectoryFiles(String directory) 
{ 
    List<File> frameworkFilePaths = []; 

    // Grab all paths in directory 
    return new Directory(directory).list(recursive: true, followLinks: false) 
    .listen((FileSystemEntity entity) 
    { 
    // For each path, if the path leads to a file, then add to array list 
    File file = new File(entity.path); 
    file.exists().then((exists) 
    { 
     if (exists) 
     { 
     frameworkFilePaths.add(file); 
     } 
    }); 

    }).asFuture().then((_) { return frameworkFilePaths; }); 

} 

Edit: OR! Ein noch besserer Ansatz (in einigen Situationen) wäre es, einen Strom von Dateien in das Verzeichnis zurückzusenden:

/** 
* Directory file stream 
* 
* Retrieve all files within a directory as a file stream. 
*/ 
Stream<File> _directoryFileStream(Directory directory) 
{ 
    StreamController<File> controller; 
    StreamSubscription source; 

    controller = new StreamController<File>(
    onListen:() 
    { 
     // Grab all paths in directory 
     source = directory.list(recursive: true, followLinks: false).listen((FileSystemEntity entity) 
     { 
     // For each path, if the path leads to a file, then add the file to the stream 
     File file = new File(entity.path); 
     file.exists().then((bool exists) 
     { 
      if (exists) 
      controller.add(file); 
     }); 
     }, 
     onError:() => controller.addError, 
     onDone:() => controller.close 
    ); 
    }, 
    onPause:() { if (source != null) source.pause(); }, 
    onResume:() { if (source != null) source.resume(); }, 
    onCancel:() { if (source != null) source.cancel(); } 
); 

    return controller.stream; 
}