function listSubfolderContents() {// Replace 'FOLDER_ID' with the actual ID of the folder you want to searchconst folderId = 'FOLDER_ID';// Get the folder object by IDconst folder = DriveApp.getFolderById(folderId);// Initialize an empty array to store subfolder names and contentsconst subfolderData = [];// Get all subfolders within the main folderconst subfolders = folder.getSubfolders();// Loop through each subfolderwhile (subfolders.hasNext()) {const subfolder = subfolders.next();// Check if subfolder name starts with "3."if (subfolder.getName().startsWith("3.")) {const subfolderName = subfolder.getName();// Get all files within the subfolderconst files = subfolder.getFiles();const fileNames = [];// Loop through each file and collect nameswhile (files.hasNext()) {const file = files.next();fileNames.push(file.getName());}// Add subfolder name and file list to the data arraysubfolderData.push({ name: subfolderName, files: fileNames });}}// Log the subfolder data to the console (optional)// Logger.log(subfolderData);// You can use the subfolderData array for further processing// (e.g., display in a spreadsheet, send an email notification)return subfolderData;
Explanation:
DriveApp.getFolderById(folderId)
.folder.getSubfolders()
.startsWith("3.")
.subfolder.getFiles()
. Then, it loops through each file and collects the names into an array called fileNames
.name
) and the list of files (files
) and pushes this object into the subfolderData
array.subfolderData
array to the console for debugging purposes (commented out). You can uncomment this line to view the results in the script editor's log. The return
statement allows you to use the subfolderData
array for further processing in your script, such as populating a spreadsheet or sending an email notification.Note:
listSubfolderContents
function.--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/59f74ab8-25f9-46e0-82a7-21a483d60a8bn%40googlegroups.com.
This email is confidential and is intended for the above-named recipient(s) only. If you are not the intended recipient, please delete this email from your system. Any unauthorized use or disclosure of this email is prohibited.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CA%2BA7ZW%2BLNUYucm9Ja0MURGz94WVYBFXNsD7DLtd7FM43ijPPKg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAJBn2DSv%2B4REMGi-VxpngFOPay3rPi2qMYs-Tbu034ZP0EEOnw%40mail.gmail.com.