如果文件夹不存在,如何创建?

时间:2014-09-30 10:44:28

标签: google-apps-script google-drive-api

我正在尝试创建一个文件夹(如果它不存在),但每次运行时代码都会创建一个新文件夹。我不知道我的代码是否正确。

这是我的代码:

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

感谢您的帮助!!

5 个答案:

答案 0 :(得分:5)

使用try / catch结构时,实际上不需要if条件。 try / catch结构处理文件夹本身不存在的情况。

试试这样:

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

答案 1 :(得分:5)

自2016年8月起的Google Apps脚本代码

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

答案 2 :(得分:2)

谷歌改变了它的脚本。我知道这是一个老帖子,但这是我能想到的最好的答案:

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

希望这有助于其他人!

答案 3 :(得分:2)

如果有其他人遇到此问题,您可以使用这样的三元运算符(根据需要替换" Name_of_Folder"字符串):

var alumnopath = DriveApp.getFoldersByName("Name_of_Folder");
var folderalumno = alumnopath.hasNext() ? 
    alumnopath.next() : DriveApp.createFolder("Name_of_Folder");

答案 4 :(得分:0)

如果有人仍在寻找此问题的答案,并且想查看文件夹(如果它不存在),那是我创建的功能。

此函数接受两个输入值:

  • base_folder-在其中执行搜索的文件夹ID,在Drive根文件夹中搜索文件夹的结果为false
  • folder_name-您要搜索的文件夹名称

有些事情可以调整,但是我已经使用它来创建文件夹结构的基本文件夹->年->月->天。

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
}

这是我使用此功能的方式:

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
}