locked
Code snippet: StorageFolder.copyFolder method RRS feed

  • 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!


    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.

    --James


    <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 AM
    Moderator
  • However I would use GetItemsAsync() | getItemsAsync() method for getting all the items and do some filters while copy.


    right, that is a good idea. I'd assume its also faster. And then use duck typing or "instance of" to decide what to do with the item.

    • 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