How to create a folder if it doesn't exist?

前端 未结 5 1784
没有蜡笔的小新
没有蜡笔的小新 2021-01-12 03:20

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:

5条回答
  •  生来不讨喜
    2021-01-12 03:54

    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);
    

提交回复
热议问题