{"id":15439,"date":"2023-08-01T09:00:00","date_gmt":"2023-08-01T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/08\/01\/exploring-subfolders-in-apps-script\/"},"modified":"2025-09-15T18:39:06","modified_gmt":"2025-09-15T09:39:06","slug":"exploring-subfolders-in-apps-script","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/08\/01\/exploring-subfolders-in-apps-script\/","title":{"rendered":"Get Subfolders&#8217; name using while"},"content":{"rendered":"<p>This post is a corrected post after AI explains the code included in the book <a title=\"\" href=\"https:\/\/www.autooffice.io\/content\/ebizbooks-apps-script\" target=\"_blank\" rel=\"noopener\">&#8220;Google Apps Script 101: Building Work Automation For Free&#8221;<\/a>. Added a <s>strikethrough<\/s> when editing what AI has written, and added <span style=\"color: #ff3366;\">color<\/span> when edited by author<\/p>\n<h2>Table of Contents<\/h2>\n<ol>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<li><a href=\"#code\">Code<\/a><\/li>\n<li><a href=\"#explanation\">Code Explanation<\/a><\/li>\n<li><a href=\"#example\">Example<\/a><\/li>\n<li><a href=\"#ai-prompt\">AI Prompt<\/a><\/li>\n<\/ol>\n<h2 id=\"summary\">Summary<\/h2>\n<p>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.<\/p>\n<h2 id=\"code\">Code<\/h2>\n<pre><code>function getSubFolders() {\n  const parentFolder = DriveApp.getFolderById('folderID') \/\/ Replace 'folderID' with the actual ID of your folder\n  const subFolders = parentFolder.getFolders()\n  \n  while (subFolders.hasNext()){\n    let subFolderName = subFolders.next().getName()    \n    Logger.log(subFolderName)    \n  }  \n}<\/code><\/pre>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>The code declares a function named <code>getSubFolders<\/code>. It starts by getting the parent folder using its ID and then retrieves the subfolders within it.<\/p>\n<p>A <code>while<\/code> loop is used to iterate over each subfolder. The name of each subfolder is retrieved using the <code>getName()<\/code> method and logged using the <code>Logger.log()<\/code> statement.<\/p>\n<p><span style=\"color: #ff3366;\"> 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 . <\/span><\/p>\n<p><span style=\"color: #ff3366;\">Function Description: <\/span><\/p>\n<p><span style=\"color: #ff3366;\">1. The `getSubFolders` function does not take a single argument. Therefore, there is no need to pass arguments when calling a function.<\/span><\/p>\n<p><span style=\"color: #ff3366;\">2. `const parentFolder = DriveApp.getFolderById(&#8216;FolderID&#8217;)`: `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 &#8216;folderid&#8217; should be replaced with the ID of the actual folder. For example, it can be used as `getFolderById(&#8216;12345abcdefg&#8217;)`.<\/span><\/p>\n<p><span style=\"color: #ff3366;\">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. <\/span><\/p>\n<p><span style=\"color: #ff3366;\">4. `while (subFolders.hasNext()) { &#8230; }`: Use the `hasNext()` method to check if the Iterator has the next item. The block of code within the &#8216;while&#8217; loop runs only while the Iterator has: <\/span><\/p>\n<p><span style=\"color: #ff3366;\">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. <\/span><\/p>\n<p><span style=\"color: #ff3366;\">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. <\/span><\/p>\n<p><span style=\"color: #ff3366;\">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 &#8216;folder ID&#8217; part. <\/span><\/p>\n<h2 id=\"example\">Example<\/h2>\n<pre><code>getSubFolders()<\/code><\/pre>\n<p>When you run the <code>getSubFolders()<\/code> function, it will retrieve the subfolders within the specified parent folder and log their names using the <code>Logger.log()<\/code> statement.<\/p>\n<h2 id=\"ai-prompt\">AI Prompt<\/h2>\n<p>Write a function that retrieves the subfolders within a specified parent folder. Log the names of the subfolders using the <code>Logger.log()<\/code> statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":15460,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[5,118],"tags":[512,69,529,7],"class_list":["post-15439","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-getfolders","tag-spreadsheet"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["ko","en"],"languages":{"ko":{"title":true,"content":true,"excerpt":true},"en":{"title":true,"content":true,"excerpt":true}}},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/comments?post=15439"}],"version-history":[{"count":3,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15439\/revisions"}],"predecessor-version":[{"id":17227,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15439\/revisions\/17227"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15460"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}