如何创建仅包含路径名的Google云端硬盘文件夹

时间:2013-08-15 01:45:54

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

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

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

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

如果我使用以下代码:

DocsList.createFolder(newFolder);

它会在根级别创建一个名为“/ Path / To My / New Folder /”

的文件夹

我的理解是正确的语法:

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

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

任何建议都非常感谢。

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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,我觉得以后会有用。我可能最终会创建一个文件夹/文件库,在创建更大规模的文件/文件夹架构时执行与此类似的任务。

答案 2 :(得分: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
}