Iterate through Google Drive Folders and Files

I
Published in: Google Apps Script - Google Drive

The Google Script will iterate through all files and folders in your Google Drive and lists the file names and the folder names inside the console. You may to use the Folder Iterator if you have too many files in the Drive else the script will exceed the allotted execution time.

function listFolders(folder) {

  folder = folder || DriveApp.getRootFolder();

  var name = folder.getName();

  var files = folder.getFiles();

  while ( files.hasNext() ) {
    Logger.log(name + " :: " + files.next().getName());
  }

  var subfolders = folder.getFolders();

  while (subfolders.hasNext()) {
    listFolders(subfolders.next());
  }

}
Published in: Google Apps Script - Google Drive

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch