1

如何仅从路径名在 Google Drive 中创建文件夹?如果文件夹或其任何子文件夹已经存在,我最好需要代码不会失败。

在我正在使用的脚本中,我只有需要在 Google Drive 中创建的文件夹的路径名:

newFolder = '/Path/To My/New Folder/';

如果我使用以下代码:

DocsList.createFolder(newFolder);

它将在根级别创建一个名为“/Path/To My/New Folder/”的文件夹

我的理解是正确的语法是:

DocsList.createFolder('Path').createFolder('To My').createFolder('New Folder');

但是,如果我只知道路径,不知道路径可以包含多少个子文件夹,并且不知道存在哪些文件夹/子文件夹,我应该使用什么代码仅从路径创建文件夹?

任何建议都非常感谢。

4

3 回答 3

4

一种更紧凑、更不容易出错的查找或创建文件夹的方法:

function createFolderFromPathName(fullPathToFolder) {
  var paths = fullPathToFolder.split("/");
  var curFolder = DriveApp.getRootFolder();
  for(var i = 0; i < paths.length; i++) {
    var folders = curFolder.getFoldersByName(paths[i]);
    curFolder = folders.hasNext() ? folders.next() : curFolder.createFolder(paths[i]);
  }  
  return curFolder;
}
于 2014-01-06T11:38:30.050 回答
3

也许是这样的?

function createFolderFromPathName(newFolder){
  var paths = newFolder.split("/");
  var curPath = "";
  var folders = DriveApp.getRootFolder().getFolders();
  var curFolder = DriveApp.getRootFolder();
  var folder;

  for(path in paths){
    var hasFolder = false;

    while(!hasFolder && folders.hasNext()){
      folder = folders.next();
      if(paths[path] == folder.getName()){
        hasFolder = true;
      } 

      if(hasFolder){
        folders = DriveApp.getFolderById(DocsList.getFolder(curPath+paths[path]).getId()).getFolders();
        curPath = curPath == "" ? paths[path] + "/" : curPath + paths[path] + "/"; 
        curFolder = DocsList.getFolder(curPath);
      } 
    } 

    if(!hasFolder){
      curFolder = curFolder.createFolder(paths[path]);
    } 
  }  
 return curFolder;
}

function test(){
 createFolderFromPathName("Folder/Design/Test"); 
}

谢谢 Serge,我想以后会有用的东西。在创建更大规模的文件/文件夹架构时,我最终可能会创建一个文件夹/文件库来执行与这些类似的任务。

于 2013-08-15T03:48:26.127 回答
0

这只是对另一个答案(user1475265)的评论,为了格式方便而写在这里

可以通过从函数返回文件夹对象来改进(一点)此代码,因此它可以直接用于创建文档...测试和(很少)修改如下:

function createFolderFromPathName(newFolder){
  var paths = newFolder.split("/");
  var curPath = "";
  var folders = DriveApp.getRootFolder().getFolders();
  var curFolder = DriveApp.getRootFolder();
  var folder, folderName;

  for(path in paths){
    var hasFolder = false;

    while(!hasFolder && folders.hasNext()){
      folder = folders.next();
      folderName = folder.getName();
      if(paths[path] == folder.getName()){
        hasFolder = true;
      } 

      if(hasFolder){
        folders = DriveApp.getFolderById(DocsList.getFolder(curPath+paths[path]).getId()).getFolders();
        curPath = curPath == "" ? paths[path] + "/" : curPath + paths[path] + "/"; 
        curFolder = DocsList.getFolder(curPath);
      } 
    } 

    if(!hasFolder){
      curFolder = curFolder.createFolder(paths[path]);
    } 
  }  
  return curFolder;// by returning the folder object it can be used directly, see test function
}

function test(){
  var curFolder = createFolderFromPathName("Folder/Design/Test"); 
  var emptyTestDoc = curFolder.createFile('testDoc','empty');// creates an empty text document
}
于 2013-08-15T07:29:04.313 回答