How to create a folder if it doesn't exist?
10,154
Solution 1
As of Google Apps Script code in 2016 Aug
var par_fdr = DriveApp.getFolderById(123456789A); // replace the ID
var fdr_name = "child_fdr";
try {
var newFdr = par_fdr.getFoldersByName(fdr_name).next();
}
catch(e) {
var newFdr = par_fdr.createFolder(fdr_name);
}
Solution 2
In case anyone else runs in to this issue, you can use the ternary operator like this (replacing the "Name_of_Folder" string as needed):
var alumnopath = DriveApp.getFoldersByName("Name_of_Folder");
var folderalumno = alumnopath.hasNext() ?
alumnopath.next() : DriveApp.createFolder("Name_of_Folder");
Solution 3
You actually don't need the if condition when you use a try/catch structure. The try/catch structure handles the case where the folder doesn't exist by itself.
Try it like this:
var alumnopath = DocsList.getFolderById ('0Bzgw8SlR34pUbFl5a2tzU2F0SUk');
var alumno2 = alumno.toString();
Logger.log(alumno2);
try{
var folderalumno = alumnopath.getFolder(alumno2);
}
catch(e) {
var folderalumno = alumnopath.createFolder(alumno2);
}
folderalumno.createFile(pdf.getAs('application/pdf')).rename( alumno + " , " + fechafor);
Solution 4
Google has changed its scripting. I know this is an old post but this is the best current answer I could come up with:
function getFolder(parent_folder,folder_name){
var folders = parent_folder.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
if(folder_name == folder.getName()) {
return folder;
}
}
return false;
}
var parent_folder = DriveApp.getFolderById(//id\\);
var folderName = getFolders(parent_folder,name);
if (folderName === false){
folderName = outputfolderId.createFolder(name);
}
Hope this helps someone else!
Author by
Mario Moreno González
Updated on June 23, 2022Comments
-
Mario Moreno González almost 2 years
I'm trying to create a folder if it doesn't exist, but the code creates a new folder every time I run it. I don´t know if my code is right.
Here is my code:
var alumnopath = DocsList.getFolderById ('0Bzgw8SlR34pUbFl5a2tzU2F0SUk'); var alumno2 = alumno.toString(); Logger.log(alumno2); try { var folderalumno = alumnopath.getFolder(alumno2); if (folderalumno == undefined){ var folderalumno = alumnopath.createFolder(alumno2); } else { var folderalumno = alumnopath.getFolder(alumno2); } } catch(e) { var folderalumno = alumnopath.createFolder(alumno2); } folderalumno.createFile(pdf.getAs('application/pdf')).rename( alumno + " , " + fechafor);
Thanks for your help!!
-
Craig about 7 yearsThis is what I do as well, but I found that getFoldersByName(fdr_name).next() was not returning an error when there was no match. I ended up adding "var name=newFdr.getName()" in the try: section to force a failure. That seemed to fix my problem, but I'm still not quite sure why it was needed.
-
Ryan almost 7 yearsThanks! It took me a while of Googling to finally find this answer, and now my script works.
-
Ron over 3 yearsUsing this answer in 2020 with the V8 engine, be aware that replacing 'var folderalumno' with 'let' or 'const' creates closure problem... declare folderalumno before the 'try' and assign the values in the try-catch.
-
asreerama over 3 yearsTried this code and works as expected but the issue is that it take 4 times as long and perhaps will take even longer with more folders in the drive. Not sure if anybody can get around this :/
-
krismans over 3 yearsSorry, but in my case, it will create max 24 folders per day and not in the same time. I'm not sure if it is related to the number of folders you have in your drive. I see that this can be improved. Maybe DriveApp.getFolders() is taking a lot of time. I would make one function call and give a date object or array with day, month, year, and get folders only once. I think it will speed up, don`t know how much faster it will be. If you still need help with this, tomorrow I can try and update this function, and you can check if it works faster or not.