I would like to create a subfolder, in the folder the user has choosen for export and save some files.
But the name of the subfolder and the name of the files will be generated automaticly - so the user only have to choose the “mainfolder” in the beginning.
Unfortunately, I can’t even create a subfolder…=(
(And if you know a way to do this without open the FileSystem again I would love to know that, too)
I’m using this to check for a subfolder and create it if it doesn’t exist. You have to pass in the folder the user has selected and it must be a sub folder. It doesn’t support nested folders.
/**
* Gets a reference to a subfolder from a folder object. Creates it if it doesn't exist.
* Does not support nested directories at this time
* @param {File} folder file of type folder
* @param {String} subfolderName name of sub folder
* @param {Boolean} create if true creates the directory if it doesn't exist
* @returns {File|Error} returns the folder or an error object
*/
async function getSubFolder(folder, subfolderName, create = true) {
var subfolder = null;
try {
subfolder = (await folder.getEntries()).find(entry => entry.name.includes(subfolderName));
if (subfolder==null || subfolder.isFolder==false) {
if (create) {
subfolder = await folder.createFolder(subfolderName);
}
}
}
catch(fileError) {
return fileError;
}
return subfolder;
}
/**
* Returns true if a subfolder exists
* @param {File} folder file of type folder
* @param {String} subfolderName name of sub folder
* @returns {Boolean} returns true if folder exists
*/
async function checkSubFolderExists(folder, subfolderName) {
var subfolder = null;
try {
subfolder = (await folder.getEntries()).find(entry => entry.name.includes(subfolderName));
if (subfolder!=null && subfolder.isFolder) {
return true;
}
}
catch(fileError) {
log(fileError);
}
return false;
}