Skip to content

Instantly share code, notes, and snippets.

@glynrob
Created February 23, 2013 19:59
Show Gist options
  • Save glynrob/5021101 to your computer and use it in GitHub Desktop.
Save glynrob/5021101 to your computer and use it in GitHub Desktop.
filesystem api create file or folder
// create a new file
function createFile(fileName){
fs.root.getFile(fileName+'.txt', {create: true, exclusive: true}, function(fileEntry) {
displayDirectory(); // reload files and folders
}, function(e) { // file was not created for some reason
if (e.code == FileError.INVALID_MODIFICATION_ERR){ // alert most common reason for failure
alert('Filename already exists');
}
});
}
// create a new folder
function createFolder(rootDirEntry, folders) {
if (folders[0] == '.' || folders[0] == '') {
folders = folders.slice(1);
}
rootDirEntry.getDirectory(folders[0], {create: true, exclusive: true}, function(dirEntry) {
// Recursively add the new subfolder (if we still have another to create).
if (folders.length) {
createFolder(dirEntry, folders.slice(1));
}
displayDirectory();
}, errorHandler);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment