When I attempt to merge two directories in AIR, if there is a conflict with any sub directories, the directory being copied to will loose any files in those sub directories.
A simple recursive function to merge the two directories (and all sub directories), keeping files from both directories.
Natively in AIR, when a file is copied to a new location via copyTo (or moved via moveTo), if the overwrite variable is false (default) the directory won't be created. To overcome this, we can set the overwrite variable to true (in the function call itself). However, this results in a complete overwrite of any sub directories with conflicting names.
| Moving | Moving to | We Get (Result of copyTo()) | We Want |
| root | root | root | root |
| subDirectory | subDirectory | subDirectory | subDirectory |
| file1.txt | file2.txt | file1.txt |
file1.txt file2.txt |
The Flex code to deliver the results we want:
private function copyInto(directoryToCopy:File, locationCopyingTo:File):void
{
var directory:Array = directoryToCopy.getDirectoryListing();
for each (var f:File in directory)
{
if (f.isDirectory)
copyInto(f, locationCopyingTo.resolvePath(f.name));
else
f.copyTo(locationCopyingTo.resolvePath(f.name), true);
}
}
+