如何使用App脚本检查父文件夹中是否存在文件夹

时间:2018-01-30 07:25:45

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

我想检查父文件夹中是否存在文件夹,如果它没有根据给定路径创建文件夹。例如原始路径Parent / Folder1现在我想在原始路径中添加一个新文件夹,就像父/ Folder1 / Folder2一样。我当前的问题是,当我给它新的路径Parent / Folder1 / Folder2时,它会重新创建包含新文件夹的整个路径,从而导致重复的文件夹。到目前为止,这是脚本。

function getOrCreateSubFolder(baseFolder,folderArray) {
    if (folderArray.length == 0) {
        return baseFolder;
    }
    var nextFolderName = folderArray.shift();
    var nextFolder = null;
    var folders = baseFolder.getFolders();
    for (var i=0; i<folders.length; i++) {
        var folder = folders[i];
        if (folders[i].getName() == nextFolderName) {
            nextFolder = folders[i];
            break;
        }
    }
    if (nextFolder == null) {
    // Folder does not exist - create it.
        nextFolder = baseFolder.createFolder(nextFolderName);
    }
    return getOrCreateSubFolder(nextFolder,folderArray);
}

/**
 * Returns the GDrive folder with the given path.
 */
function getFolderByPath(path) {
    var parts = path.split("/");

  if (parts[0] == '') parts.shift(); // Did path start at root, '/'?

  var folder = DriveApp.getRootFolder();
  for (var i = 0; i < parts.length; i++) {
    var result = folder.getFoldersByName(parts[i]);
    if (result.hasNext()) {
        folder = result.next();
    } else {
        throw new Error( "folder not found." );
    }
  }
  return folder;
}

/**
 * Returns the GDrive folder with the given name or creates it if not existing.
 */
function getOrCreateFolder(folderName) {
    var folder;
    try {
        folder = getFolderByPath(folderName);
    } catch(e) {
        var folderArray = folderName.split("/");
        folder = getOrCreateSubFolder(DriveApp.getRootFolder(), folderArray);
    }
    return folder;
}

1 个答案:

答案 0 :(得分:1)

试试这个:

function getOrCreateSubFolder(childFolderName, parentFolderName) {
  var parentFolder, parentFolders;
  var childFolder, childFolders;
  // Gets FolderIterator for parentFolder
  parentFolders = DriveApp.getFoldersByName(parentFolderName);
  /* Checks if FolderIterator has Folders with given name
  Assuming there's only a parentFolder with given name... */ 
  while (parentFolders.hasNext()) {
    parentFolder = parentFolders.next();
  }
  // If parentFolder is not defined it sets it to root folder
  if (!parentFolder) { parentFolder = DriveApp.getRootFolder(); }
  // Gets FolderIterator for childFolder
  childFolders = parentFolder.getFoldersByName(childFolderName);
  /* Checks if FolderIterator has Folders with given name
  Assuming there's only a childFolder with given name... */ 
  while (childFolders.hasNext()) {
    childFolder = childFolders.next();
  }
  // If childFolder is not defined it creates it inside the parentFolder
  if (!childFolder) { parentFolder.createFolder(childFolderName); }
  return childFolder;
}

function test() {
  // Call it looking up in Drive root folder
  var a = getOrCreateSubFolder('CHILD');
  // Call it looking up in given parent folder
  var b = getOrCreateSubFolder('CHILD', 'PARENT');
}