Search code examples
windows-8windows-store-appswinjs

WinJS Creating multiple folders


Okay, so I'm trying to create a deep folder structure and I'm looping through duplicate folder names and creating promises like this:

var promises = [];
_.each(files, function (file) {
    promises.push(localFolder
        .createFolderAsync(folder1Name, Windows.Storage.NameCollisionOption.OpenIfExists)
        .then(function (folder1) {
            return folder1.createFolderAsync(folder2Name, Windows.Storage.CreationCollisionOption.OpenIfExists);
        })
        .then(function (folder2) {
            return folder2.createFileAsync(fileName, Windows.Storage.NameCollisionOption.replaceExisting)
        })
    );
});

return WinJS.Promise.join(promises);

The problem is the duplicate folders, I thought OpenIfExists would just return the existing folders, but instead I end up with folders called "folder1Name (1)", "folder1Name (2)", etc. Using FailIfExists also is not failing, so I suspect there's something wrong with my promise chain.

Can anyone pinpoint what I'm doing wrong?


Solution

  • The second parameter of the first createFolderAsync call should be Windows.Storage.CreationCollisionOption.openIfExists instead of NameCollisionOption.

    var promises = [];
    _.each(files, function (file) {
        promises.push(localFolder
            .createFolderAsync(folder1Name, Windows.Storage.CreationCollisionOption.openIfExists)
            .then(function (folder1) {
                return folder1.createFolderAsync(folder2Name, Windows.Storage.CreationCollisionOption.openIfExists);
            })
            .then(function (folder2) {
                return folder2.createFileAsync(fileName, Windows.Storage.NameCollisionOption.replaceExisting)
            })
        );
    });
    
    return WinJS.Promise.join(promises);