rryanp
rryanp

Reputation: 1027

Iterate Through Folders/Subfolders/Files in Google Drive

I have a Google Drive structure setup like this:

Client A
--Project 1
----Drafts
----Presentations
----Minutes
--Project 2
----Drafts
----Presentations
----Minutes
Client B
<and so on>

I want to write a script where I can pass in the ID of Project 1, and it will loop through each of the three subfolders and count the total number of files (ultimately I want to count the number of files updated over a date range, but baby steps!).

I've tried using the Class FolderIterator code, but that wants you to assign a group of folders to a variable "folders" (it showed var folders = DriveApp.getFolders(); and I tried var folders = DriveApp.getFolderById("<id of Project 1 here>"); but in my version that's just one folder, and it really needs the collection of folders). Is there a way to assign those three subfolders to that folders variable if I have the ID of the parent folder? Or is there a better way to loop through the subfolders?

Upvotes: 3

Views: 5621

Answers (2)

Serge insas
Serge insas

Reputation: 46794

As a complement to Bryan's answer (thx, I upvoted) I wrote a test function to see results in the logger and to run the function with appropriate parameters to get the count you wanted.

(This answer should not be selected as the right answer since the main part is from Bryan P)

here is how it goes :

    function testTraverse(){
      var originFolder = DriveApp.getFoldersByName('Client A').next(); // use the folder name you want here
      var totalCount = traverseFolder(originFolder,0);
      Logger.log('total files = '+totalCount);// note that if some files are
      // in more than one subfolder the count will count every occurrence of 
      //this file so the total might be > to the sum of intermediate values
    }

    function traverseFolder(folder,total) {
      var name = folder.getName();
      var count = 0;
      var files = folder.getFiles();

      while (files.hasNext()) {
        count++; 
      Logger.log('fileName ('+count+') in '+name+' is : '+files.next().getName());

      }
      Logger.log('\n'+name+' has ' + count+' files\n----------------------\n' );
      var subs = folder.getFolders();
      while (subs.hasNext()) {
        total+=traverseFolder(subs.next(),count);
      }
      return total;
    }

Note that this code will work for a "reasonable" number of files, if you have many files and folders it might exceed the 5 minutes execution time limit. In this case you'll need to modify it in order to proceed by smaller bunches of files using the token provided by the method.

Upvotes: 2

Bryan P
Bryan P

Reputation: 5051

It may look some thing like this with DriveApp...

function traverseFolder(folder) {
  var name = folder.getName();

  if ( /^(Drafts|Presentations|Minutes)$/.test(name) ) { 
    var count = 0;
    var files = folder.getFiles();

    while ( files.hasNext() ) {
      count++; 
    }
    Logger.log(name + ' : ' + count);
  }
  var subs = folder.getFolders();

  while (subs.hasNext()) {
    traverseFolder(subs.next());
  }
}

We now have access to the Advanced Drive Service too. Maybe I'll look at an example with that if some one doesn't post one soon.

Upvotes: 1

Related Questions