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

前端 未结 5 1783
没有蜡笔的小新
没有蜡笔的小新 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:49

    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!

    0 讨论(0)
  • 2021-01-12 03:50

    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);
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-12 03:55

    If someone is still looking for answer on this question and want to cerate folder if it doesn`t exist here is function I have created.

    This function accepts two input values:

    • base_folder - folder ID in which to perform a search or false to search folders in Drive root folder
    • folder_name - folder name that you are searching for

    There is some things that can be adjusted, but I have using this to create folder structure base folder -> year -> month -> day.

    function checkFolders(base_folder, folder_name) {
      if (base_folder){ //Checks if is specified main folder
        var folders = DriveApp.getFolderById(base_folder).getFolders(); //Getting folder list from specified location
      } else { //If not specified folder we take all Drive folders
       var folders = DriveApp.getFolders(); //Getting all folders in Drive
      }
    
      var folder_exist = false, folder; //By default folder don`t exists till we check and confirm that it exists
    
      while(folders.hasNext()){ //Looping through all folders in specified location while we have folders which to check
        folder = folders.next(); //Taking next folder to check if it is folder that we are looking for
        if(folder.getName() == folder_name){ //Checking it`s name
         folder_exist = folder.getId(); //If folder is found we assign its ID to our return variable
        } //If folder don`t exists we have already assigned false to return value
      }
    
      if (!folder_exist) { //Checks if folder don`t exists than we need to create it
        if(base_folder){ //Checking where we should create folder
          DriveApp.getFolderById(base_folder).createFolder(folder_name); //If base folder is specified than we create there folder
        } else {
          DriveApp.createFolder(folder_name); //If base folder not specified than we create folder in Drive root folder
        }
       folder_exist = checkFolders(base_folder, folder_name); //Now run self again to check if folder was created and if it is created than we get folder ID
      }
    
      return folder_exist; //Returning false if our folder is not created or folder ID if folder exists and is created
    }
    

    This is how I`m using this function:

    function createFolderStructure() {
     var base_folder_id, year_folder_id, month_folder_id, day_folder_id, date, day, month, year;
    
     date = new Date(); //Creates new Date object
     day = date.getDate(); //Extract day number in month
     month = date.getMonth(); //Extract month number, first month number is 0
     year = date.getFullYear(); //Extract year number
    
     base_folder_id = checkFolders(false, "My base folder"); //Check if base folder is created, if created return folder ID, if not, than create it and return it ID
     year_folder_id = checkFolders(base_folder_id, year); //Check if year folder is created, if created return folder ID, if not, than create it and return it ID
     month_folder_id = checkFolders(year_folder_id, month); //Check if month folder is created, if created return folder ID, if not, than create it and return it ID
     day_folder_id = checkFolders(month_folder_id, day); //Check if day folder is created, if created return folder ID, if not, than create it and return it ID, ID can be used to save files in this folder
    }
    
    0 讨论(0)
  • 2021-01-12 04:07

    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");
    
    0 讨论(0)
提交回复
热议问题