Asked by:
Code snippet: StorageFolder.copyFolder method

General discussion
-
until now :) Needed one, so I came up with this:
copyFolderAsync = function(sourceFolder, destFolder) { return destFolder.createFolderAsync(sourceFolder.name, CreationCollisionOption.openIfExists).then(function(destSubFolder) { return sourceFolder.getFilesAsync().then(function(files) { return WinJS.Promise.join(files.map(function(file) { return file.copyAsync(destSubFolder, file.name, NameCollisionOption.replaceExisting); })); }).then(function() { return sourceFolder.getFoldersAsync(); }).then(function(folders) { return WinJS.Promise.join(folders.map(function(folder) { return copyFolderAsync(folder, destSubFolder); })); }); }); };
Copies recursively and copies files over. Could be extended to skip existing files (by size and date and/or content).
I especially like the non-recursion because of the usage of Promises.
Enjoy!
- Changed type pkursawe Friday, June 13, 2014 3:25 PM
- Edited by Rob Caplan [MSFT]Microsoft employee, Moderator Monday, June 16, 2014 6:04 PM clarified title
Friday, June 13, 2014 3:25 PM
All replies
-
Hi pkursawe,
Your suggestion for the API is a good idea, I will submit to senior engineers and I think you could also report at here:http://wpdev.uservoice.com/forums/110705-dev-platform.
However I would use GetItemsAsync() | getItemsAsync() method for getting all the items and do some filters while copy.
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.Wednesday, June 18, 2014 9:34 AMModerator -
However I would use GetItemsAsync() | getItemsAsync() method for getting all the items and do some filters while copy.
- Edited by pkursawe Wednesday, June 18, 2014 9:47 AM
Wednesday, June 18, 2014 9:46 AM -
What you think, should I copy the file items sequentially, or use the WinJS.Promise.join (meaning firing all copy actions at once).
On non-SSDs sequential copying might be faster.
copyFolderAsync = function(destFolder, sourceFolder) { return destFolder.createFolderAsync(sourceFolder.name, CreationCollisionOption.openIfExists).then(function(destSubFolder) { return sourceFolder.getItemsAsync().then(function(items) { return WinJS.Promise.join(items.map(function(item) { if (item instanceof Windows.Storage.StorageFile) { return item.copyAsync(destSubFolder, item.name, NameCollisionOption.replaceExisting); } else { return copyFolderAsync(destSubFolder, item); } })); }); }); };
- Edited by phil_ke Wednesday, June 18, 2014 8:49 PM
Wednesday, June 18, 2014 8:48 PM -
Posted there with back-reference to this place here:
http://wpdev.uservoice.com/forums/110705-dev-platform/suggestions/6071259-add-storagefolder-copyfolderasync
Vote if you care :)
Wednesday, June 18, 2014 10:38 PM