Recently, I stumbled upon a script located at https://github.com/bluexm/snipets/blob/master/list%20Gdrive%20files%20and%20folders. The challenge I encountered was that the script was not capable of handling folders with identical names. As a result, I made some modifications to restrict the search to a specific folder. Unfortunately, the script only delves into one level of subfolders, failing to explore deeper levels such as 'Client Documents'/'2021-01-25'/'Subfolder1'/'Sub-Subfolder2'.
I'm perplexed as to why the script is unable to proceed to Sub-Subfolder1. Any insights or suggestions on this issue would be greatly appreciated.
function ListFilesandFolders(){
var sheet = ss.getActiveSheet();
var fileno = sheet.getSheetName();
var filenoFolders = DriveApp.searchFolders("title contains '"+fileno.replace("'","\'")+"' and trashed = false and hidden = false");
var filenoFolder = filenoFolders.next().getFoldersByName("Internal Documents").next();
var clientdocsFolders = filenoFolder.getFoldersByName("Disclosure").next().getFoldersByName("Our Client").next().getFolders();
var clientdocsFolder = filenoFolder.getFoldersByName("Disclosure").next().getFoldersByName("Our Client").next();
var aorFolder = clientdocsFolder.getFoldersByName("Affidavit of Records").next();
var tz = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var date = Utilities.formatDate(new Date(), tz, "yyyy-MM-dd");
// Find or Create doclist
var aorFolderlist = aorFolder.getFiles();
var doclist = "new"
while (aorFolderlist.hasNext()) {
var aorFile = aorFolderlist.next();
if (aorFile.getName().includes("Document List")) {
var doclist = SpreadsheetApp.open(aorFile);
// doclist.duplicateActiveSheet().activate();
// doclist.renameActiveSheet(date);
// doclist.moveActiveSheet(0)
}
}
if (doclist == "new") {
var doclist = SpreadsheetApp.create(date + " Document List");
doclist.renameActiveSheet(date);
doclist.appendRow(["Document No.", "Document Date", "Document Title", "File Name", "URL", "Folder", "Date Created", "File ID", "File Name (No Date)", "Producible"]);
DriveApp.getFileById(doclist.getId()).moveTo(aorFolder);
}
ListFilesandFolders1F(clientdocsFolders,'',doclist)
}
function ListFilesandFolders1F(fn,rfn,sheet) {
// declare an array to push data into the spreadsheet
var data = [];
var count = 1;
var foldersnext = fn.next();
Logger.log("THE FOLDER IS "+foldersnext);// DEBUG
// list files in this folder
// myfiles is a File Iterator
var myfiles = foldersnext.getFiles();
Logger.log("FILES IN THIS FOLDER"); //DEBUG
// loop through files in this folder
while (myfiles.hasNext()) {
var myfile = myfiles.next();
count = Number(count) + 1;
var shno = "#";
var shdate = '=LEFT(D' + count + ',FIND(" ",D' + count + ')-1)';
var shtitle = '=LEFT(I' + count + ',FIND(".",I' + count + ')-1)';
var shfilename = '=RIGHT(D' + count + ',LEN(D' + count + ')-FIND(" ",D' + count + '))';
var fname = myfile.getName();
var fdate = myfile.getDateCreated();
var fid = myfile.getId();
var furl = myfile.getUrl();
// Populate the array for this file
data = [
shno,
shdate,
shtitle,
fname,
furl,
foldersnext,
fdate,
fid,
shfilename
];
//Logger.log("data = "+data); //DEBUG
sheet.appendRow(data);
} // Completes listing of the files in the named folder
// Now get the subfolder
// subfolders is a Folder Iterator
var subfolders = foldersnext.getFolders();
Logger.log("THE SUBFOLDER(S) ARE"); //DEBUG HEADING
// now start a loop on the SubFolder list
while (subfolders.hasNext()) {
var subfolderdata = [];
var mysubfolders = subfolders.next().getFolders();
var mysubfolder = mysubfolders.next();
ListFilesandFolders1F(mysubfolders,rfn +'/'+ mysubfolder,sheet)
}
}