Get Subfolders’ name using while
This post is a corrected post after AI explains the code included in the book “Google Apps Script 101: Building Work Automation For Free”. Added a strikethrough when editing what AI has written, and added color when edited by author
Table of Contents
Summary
In this blog post, we will explore how to retrieve subfolders within a parent folder using Apps Script. We will learn how to access the subfolders and retrieve their names.
Code
1 2 3 4 5 6 7 8 9 |
function getSubFolders() { const parentFolder = DriveApp.getFolderById('folderID') // Replace 'folderID' with the actual ID of your folder const subFolders = parentFolder.getFolders() while (subFolders.hasNext()){ let subFolderName = subFolders.next().getName() Logger.log(subFolderName) } } |
Code Explanation
The code declares a function named getSubFolders
. It starts by getting the parent folder using its ID and then retrieves the subfolders within it.
A while
loop is used to iterate over each subfolder. The name of each subfolder is retrieved using the getName()
method and logged using the Logger.log()
statement.
The above code uses Google Apps Script to get the names of subfolders of a specific folder from Google Drive and outputs them as logs using Logger, getSubFolders
. This is an example of defining .
Function Description:
1. The getSubFolders
function does not take a single argument. Therefore, there is no need to pass arguments when calling a function.
2. const parentFolder = DriveApp.getFolderById('FolderID')
: DriveApp
is an object that accesses the Google Drive service. Use the getFolderById()
method to get a reference to the folder with the specified folder ID. Here ‘folderid’ should be replaced with the ID of the actual folder. For example, it can be used as getFolderById('12345abcdefg')
.
3. const subFolders = parentFolder.getFolders()
: Gets an Iterator object for all subfolders under the folder stored in the parentFolder
variable. The getFolders()
method returns an Iterator.
4. while (subFolders.hasNext()) { ... }
: Use the hasNext()
method to check if the Iterator has the next item. The block of code within the ‘while’ loop runs only while the Iterator has:
5. let subFolderName = subFolders.next().getName()
: Use the next()
method to get the next subfolder from the Iterator, and the getName()
method to get the name of that folder. The name of the imported subfolder is stored in the subFolderName
variable.
6. Logger.log(subFolderName)
: Use Logger
to log the name of the subfolder stored in the subFolderName
variable. In this way, the names of subfolders are output to the log one by one.
Now if you call the function getSubFolders
, the names of the subfolders of the specified folder will be printed to the log. Note that in order to execute the code, you must write the script in the Google Apps Script editor, find out the desired folder ID from Google Drive, and replace the ‘folder ID’ part.
Example
1 |
getSubFolders() |
When you run the getSubFolders()
function, it will retrieve the subfolders within the specified parent folder and log their names using the Logger.log()
statement.
AI Prompt
Write a function that retrieves the subfolders within a specified parent folder. Log the names of the subfolders using the Logger.log()
statement.