0

EDIT: Have now attempted to run the Tedinoz code found in forums here and am able to get filenames but only from two folders within the main folder... The two folders it's NOT reading, have several child folders. IS there a known limit to the number of child folders that can be acquired from Google App Script??

I have a script from @mesgarpour that I'm running on Google Drive to list out all assets therein. I have modified slightly to gain recursion down to the lowest file levels. But, while it runs, it is only listing the folders and subfolders AND NOT the files inside them. I am pasting the code below and would appreciate any help. AGAIN: I am looking for the script to list all files along with the other data that I'm getting.

Have attached image shows the style of folder names as well as the fact that only folders are being shown by the script.

// TODO: Set folder ID
var folderId = 'My folder ID';
 
// Main function 1: List all folders, & write into the current sheet.
function listFolers(){
  getFolderTree(folderId, false);
};

// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
  getFolderTree(folderId, true); 
};

// =================
// Get Folder Tree
function getFolderTree(folderId, listAll) {
  try {
    // Get folder by id
    var parentFolder = DriveApp.getFolderById(folderId);
    
    // Initialise the sheet
    var file, data, sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    sheet.appendRow(["Full Path", "Name", "Date", "URL", "Last Updated", "Description", "Size"]);
    
    // Get files and folders
    getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);
    
  } catch (e) {
    Logger.log(e.toString());
  }
};

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();
  
  // List folders inside the folder
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    // Logger.log("Folder Name: " + childFolder.getName());
    data = [ 
      parentName + "/" + childFolder.getName(),
      childFolder.getName(),
      childFolder.getDateCreated(),
      childFolder.getUrl(),
      childFolder.getLastUpdated(),
      childFolder.getDescription(),
      childFolder.getSize()
    ];
    // Write
    sheet.appendRow(data);
    
    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      // Logger.log("File Name: " + childFile.getName());
      data = [ 
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        childFile.getDateCreated(),
        childFile.getUrl(),
        childFile.getLastUpdated(),
        childFile.getDescription(),
        childFile.getSize()
      ];
      // Write
      sheet.appendRow(data);
    }
    
    // Recursive call of the subfolder
    getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
  }
};

the attached image shows the style of folder names as well as the fact that only folders are being shown by the script

17
  • RELATED: Google script listing folders, subfolders and files
    – Tedinoz
    Commented Sep 22, 2022 at 3:28
  • Does this answer your question? Get a list of Drive files into a spreadsheet
    – Tedinoz
    Commented Sep 22, 2022 at 3:29
  • Hi @Tedinoz it does not go beyond listing the folders and even then doesn't list all of them. The code I posted goes a bit further. What I need is how to press the recursion further. Commented Sep 24, 2022 at 1:20
  • Um. The “related” script “will create a List with FolderName, FolderID, Filename and FileID of all Folders and Files included in a Google Drive Fiolder”. The “Does this answer?” includes a image that displays folder and sub-folders AND file names. Why debug your script when there are tried-and-tested scripts that will do EXACTLY what you are trying to achieve.
    – Tedinoz
    Commented Sep 24, 2022 at 11:04
  • It is not unfortunately, hence my tinkering. I'm using the script that you @Tedinoz provided in your response to me, unadulterated save for changing the folderID and it is only giving me the lowest folder level, but not files within the folders. I'd already searched for and found the script when the one I found initially didn't go far enough. Not sure why its not working, would love any ideas anyone has about solving it. Commented Sep 27, 2022 at 0:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.