I have a folder in my drive which houses multiple "Client" folders. Each client folder has the same structure which includes a folder that contains completed invoices (called "Completed invoices").
I need a way to iterate through all the folders named "Completed Invoices" and list in a google sheet the spreadsheet Id's in those folders so I can loop through those files later and extract data from them.
Ive found multiple code sources that work but only on the first level - i.e. it will list the files in the agents/clients folder, but it will not go into the sub folders. E.g. see code below
function listFilesInFolder() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);
var folder = DriveApp.getFoldersByName("FOLDER ID GOES HERE");
//Logger.log(folder);
var contents = folder.getFiles();
var cnt = 0;
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
Logger.log(file);
Logger.log(cnt);
data = [
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
,
];
sheet.appendRow(data);
};
};
Make a new folder called 'Completed Invoices' and add the invoices to that folder. Then you only need to search in a single folder. This is possible because folders in Drive are simply tags and a file can have multiple parent folders.
Thanks everyone for your answers, but after some more searching I found my answer. For anyone who is looking for a similar solution - I found an excellent script here which provides a really robust output that I imagine could be used for a variety of different scenarios:
Code above may be depricated (didn't work for me). You can try this and then view the output in the execution logs (accessed via hotkey CTRL + ENTER)