1
votes

How do I create a folder in Google Drive from only a pathname? Preferably I need the code to not fail if either the folder or any of it's sub-folders already exist.

In the script I'm working in I only have the path name to a folder that I need to create in Google Drive:

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

If I use the following code:

DocsList.createFolder(newFolder);

It will create a folder at the root level called "/Path/To My/New Folder/"

My understanding is the correct syntax would be:

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

However if I only know the path, do not know how many sub-folders the path could contain, and do not know which of the folders/ sub folders exist what code should I use to create the folder from only a path?

Any suggestions much appreciated.

3

3 Answers

4
votes

A far more compact, less error-prone way to find or create a folder:

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;
}
3
votes

Maybe something like this?

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

Thanks Serge, I figured something like it would be useful later on. I might end up creating a Folder/File library that does tasks similar to these when creating larger scale file/folder architectures.

0
votes

This is only a comment on the other answer (user1475265), written here for format convenience

this code can be improved (a bit) by returning the folder object from the function so it can be used directly to create a document... test and (very little ) modification below :

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
}