6

I've been using DocsList for a big project and it was working perfectly. Lately, bugs have been popping up and they mostly have roots with getting a folder or file. When I did research, I found that DriveApp had been updated. The problem is that DriveApp doesn't have search parameters like DocsList had.

For example, if I had a folder structure like this:

Root
-Main Folder 1
--Folder 1
--Folder 2
-Main Folder 2
--Folder 1
--Folder 2

To get folder "Folder 1" in "Main Folder 2," I could put in the search parameter like so: DocsList.getFolder('Main Folder 2/Folder 1')

With DriveApp, I just can't understand how to work with it. From what I understand, I have to do something like this for DriveApp:

var mainFolders = DriveApp.getFoldersByName('Main Folder 2');
while (mainFolders.hasNext()) {
  var mainFolder = termFolders.next();
  var subFolders = termFolder.getFoldersByName('Folder 1');
  // Something like this...
}

So if I had a folder that is more "deep" I would have to expand this even further..?

I feel like instead of making things easier, they made it more complicated with all the FileIterators and FolderIterators. And just making it hard to "get" a file or folder in code terms.

So basically, the point of this thread is to find out how a person who is use to DocsList to navigate and edit Drive files/folders can migrate to DriveApp and achieve the same things.

Small/Discrete examples of different scenarios would be really helpful. I can take it from there. I'll edit this more, if you guys think I'm not being clear about what I need help on.

Kara
  • 6,115
  • 16
  • 50
  • 57
PhysLabTsar
  • 256
  • 2
  • 5
  • 11
  • Good question... I'm not sure why but I assumed as DocsList was 'Experimental!' that it would be replacing DriveApp. Found [this link to a discussion at Google I/O](http://youtu.be/0HVJMIeb3aE?t=34m45s) where @ArunNagarajan recommends switching over. Your example seems to be the correct way of going about it but I can see this taking longer and approaching the maximum execution time, which is why I assume [`getContinuationToken()`](https://developers.google.com/apps-script/reference/drive/folder-iterator#getContinuationToken()) was added. I too will miss the `getFolder(path)` method. – dev Nov 24 '13 at 23:55
  • A little on the subject: https://plus.google.com/114246942750290439721/posts/duahLW1kRyF, https://plus.google.com/104812769727428324010/posts/D9wJJittDhb, https://code.google.com/p/google-apps-script-issues/issues/detail?id=3045 – wchiquito Nov 25 '13 at 06:32
  • A related stackoverflow question at the following link: [How to update DocsList to DriveApp in my code](http://stackoverflow.com/a/29778417/2946873) – Alan Wells Jun 08 '15 at 14:15

2 Answers2

7

The discussions from wchiquito's comment are an interesting read, but following all the links is time-consuming.

Bottom line: There will not be DriveApp version of getFolderByPath(), so you will need to roll your own. In the Google+ group, Faustino proposed a work-around and Eric improved it. Here it is, with an added check to allow paths that start with "/".

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 {
      return null;
    }
  }
  return folder;
}

With that, you can simply do myFolder = getFolderByPath('Main Folder 2/Folder 1');. You will end up with a DriveApp Folder instance.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
2

The code below works at mine. It is based on having the same Id

function convertFileFromDocsListToDriveApp(file)
{ // Because of difference between DocsList and DriveApp
   return (file === null) ? null : DriveApp.getFileById(file.getId());
}  

function convertFolderFromDocsListToDriveApp(folder)
{ // Because of difference between DocsList and DriveApp
  return (folder === null) ? null : DriveApp.getFolderById(folder.getId());
}  

I call this in a few 'strategic' positions in my code. I didn't test conversion from DriveApp to DocsList, but I expect this to work as well.

SoftwareTester
  • 1,048
  • 1
  • 10
  • 25