Iterating through folders / subfolders / files in Google Drive

I have a google drive structure structure like:

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 Project ID 1 and it will go through each of the 3 subfolders and count the total number of files (Ultimately I want to count the number of updated files over a date range, but childish steps!).

I tried using the code of the FolderIterator class, but you need to assign a folder group to the "folders" variable (it showed var folders = DriveApp.getFolders();

and I tried var folders = DriveApp.getFolderById("<id of Project 1 here>");

, but my version only has one folder and it really needs a folder collection). Is there a way to assign these three subfolders to this variable folders

if I have the parent folder id? Or is there a better way to iterate over the subfolders?

+3


source to share


2 answers


It might seem like something like 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 . Maybe I'll see an example with this in case someone doesn't post soon.

+1


source


As a complement to Bryan's answer (thanks, I'm upvoted) I wrote a test function to see the results in the log and run the function with the appropriate parameters to get the desired score.

(This answer should not be chosen as the correct answer, as the bulk is by Brian R.)

this is how it works:



    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 a lot of files and folders it may exceed the 5 minute runtime limit. In this case, you will need to change it to navigate to smaller file bundles using the marker provided by the method.

+2


source







All Articles