Class Folder

Folder

A folder in Google Drive. Folders can be accessed or created fromDriveApp.

// Log the name of every folder in the user's Drive.constfolders=DriveApp.getFolders();while(folders.hasNext()){constfolder=folders.next();Logger.log(folder.getName());}

Methods

MethodReturn typeBrief description
addEditor(emailAddress)FolderAdds the given user to the list of editors for theFolder.
addEditor(user)FolderAdds the given user to the list of editors for theFolder.
addEditors(emailAddresses)FolderAdds the given array of users to the list of editors for theFolder.
addViewer(emailAddress)FolderAdds the given user to the list of viewers for theFolder.
addViewer(user)FolderAdds the given user to the list of viewers for theFolder.
addViewers(emailAddresses)FolderAdds the given array of users to the list of viewers for theFolder.
createFile(blob)FileCreates a file in the current folder from a givenBlob of arbitrary data.
createFile(name, content)FileCreates a text file in the current folder with the given name and contents.
createFile(name, content, mimeType)FileCreates a file in the current folder with the given name, contents, and MIME type.
createFolder(name)FolderCreates a folder in the current folder with the given name.
createShortcut(targetId)FileCreates a shortcut to the provided Drive item ID, and returns it.
createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)FileCreates a shortcut to the provided Drive item ID and resource key, and returns it.
getAccess(email)PermissionGets the permission granted to a specific user.
getAccess(user)PermissionGets the permission granted to a specific user.
getDateCreated()DateGets the date theFolder was created.
getDescription()StringGets the description for theFolder.
getEditors()User[]Gets the list of editors for thisFolder.
getFiles()FileIteratorGets a collection of all files that are children of the current folder.
getFilesByName(name)FileIteratorGets a collection of all files that are children of the current folder and have the given name.
getFilesByType(mimeType)FileIteratorGets a collection of all files that are children of the current folder and have the given MIME type.
getFolders()FolderIteratorGets a collection of all folders that are children of the current folder.
getFoldersByName(name)FolderIteratorGets a collection of all folders that are children of the current folder and have the given name.
getId()StringGets the ID of theFolder.
getLastUpdated()DateGets the date theFolder was last updated.
getName()StringGets the name of theFolder.
getOwner()UserGets the owner of thisFolder.
getParents()FolderIteratorGets a collection of folders that are immediate parents of theFolder.
getResourceKey()StringGets the resource key of theFolder that is required to access items thathave been shared using a link.
getSecurityUpdateEligible()BooleanGets whether thisFolder is eligible to apply the security update thatrequires a resource key for access when it's shared using a link.
getSecurityUpdateEnabled()BooleanGets whether thisFolder requires a resource key for access when it'sshared using a link.
getSharingAccess()AccessGets which class of users can access theFolder, besides any individualusers who have been explicitly given access.
getSharingPermission()PermissionGets the permission granted to those users who can access theFolder,besides any individual users who have been explicitly given access.
getSize()IntegerGets the number of bytes used to store theFolder in Drive.
getUrl()StringGets the URL that can be used to open theFolder in a Google App likeDrive or Docs.
getViewers()User[]Gets the list of viewers and commenters for thisFolder.
isShareableByEditors()BooleanDetermines whether users with edit permissions to theFolder are allowed toshare with other users or change the permissions.
isStarred()BooleanDetermines whether theFolder has been starred in the user's Drive.
isTrashed()BooleanDetermines whether theFolder is in the trash of the user's Drive.
moveTo(destination)FolderMoves this item to the provided destination folder.
removeEditor(emailAddress)FolderRemoves the given user from the list of editors for theFolder.
removeEditor(user)FolderRemoves the given user from the list of editors for theFolder.
removeViewer(emailAddress)FolderRemoves the given user from the list of viewers and commenters for theFolder.
removeViewer(user)FolderRemoves the given user from the list of viewers and commenters for theFolder.
revokePermissions(emailAddress)FolderRevokes the access to theFolder granted to the given user.
revokePermissions(user)FolderRevokes the access to theFolder granted to the given user.
searchFiles(params)FileIteratorGets a collection of all files that are children of the current folder and match the given searchcriteria.
searchFolders(params)FolderIteratorGets a collection of all folders that are children of the current folder and match the given searchcriteria.
setDescription(description)FolderSets the description for theFolder.
setName(name)FolderSets the name of theFolder.
setOwner(emailAddress)FolderChanges the owner of theFolder.
setOwner(user)FolderChanges the owner of theFolder.
setSecurityUpdateEnabled(enabled)FolderSets whether theFolder requires a resource key for access when it'sshared using a link.
setShareableByEditors(shareable)FolderSets whether users with edit permissions to theFolder are allowed to sharewith other users or change the permissions.
setSharing(accessType, permissionType)FolderSets which class of users can access theFolder and what permissionsthose users are granted, besides any individual users who have been explicitly given access.
setStarred(starred)FolderSets whether theFolder is starred in the user's Drive.
setTrashed(trashed)FolderSets whether theFolder is in the trash of the user's Drive.

Deprecated methods

MethodReturn typeBrief description
addFile(child)FolderAdds the given file to the current folder.
addFolder(child)FolderAdds the given folder to the current folder.
removeFile(child)FolderRemoves the given file from the current folder.
removeFolder(child)FolderRemoves the given folder from the current folder.

Detailed documentation

addEditor(emailAddress)

Adds the given user to the list of editors for theFolder. If the user was alreadyon the list of viewers, this method promotes the user out of the list of viewers.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

addEditor(user)

Adds the given user to the list of editors for theFolder. If the user was alreadyon the list of viewers, this method promotes the user out of the list of viewers.

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

addEditors(emailAddresses)

Adds the given array of users to the list of editors for theFolder. If any of theusers were already on the list of viewers, this method promotes them out of the list ofviewers.

Parameters

NameTypeDescription
emailAddressesString[]An array of email addresses of the users to add.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

addViewer(emailAddress)

Adds the given user to the list of viewers for theFolder. If the user was alreadyon the list of editors, this method has no effect.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

addViewer(user)

Adds the given user to the list of viewers for theFolder. If the user was alreadyon the list of editors, this method has no effect.

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

addViewers(emailAddresses)

Adds the given array of users to the list of viewers for theFolder. If any of theusers were already on the list of editors, this method has no effect for them.

Parameters

NameTypeDescription
emailAddressesString[]An array of email addresses of the users to add.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

createFile(blob)

Creates a file in the current folder from a givenBlob of arbitrary data.

Parameters

NameTypeDescription
blobBlobSourceThe data for the new file.

Return

File — The new file.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

createFile(name, content)

Creates a text file in the current folder with the given name and contents. Throws anexception ifcontent is larger than 50 MB.

// Create a text file with the content "Hello, world!"DriveApp.getRootFolder().createFile('New Text File','Hello, world!');

Parameters

NameTypeDescription
nameStringThe name of the new file.
contentStringThe content for the new file.

Return

File — The new file.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

createFile(name, content, mimeType)

Creates a file in the current folder with the given name, contents, and MIME type. Throwsan exception ifcontent is larger than 10MB.

// Create an HTML file with the content "Hello, world!"DriveApp.getRootFolder().createFile('New HTML File','<b>Hello, world!</b>',MimeType.HTML);

Parameters

NameTypeDescription
nameStringThe name of the new file.
contentStringThe content for the new file.
mimeTypeStringThe MIME type of the new file.

Return

File — The new file.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

createFolder(name)

Creates a folder in the current folder with the given name.

Parameters

NameTypeDescription
nameStringThe name of the new folder.

Return

Folder — The new folder.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

createShortcut(targetId)

Creates a shortcut to the provided Drive item ID, and returns it.

Parameters

NameTypeDescription
targetIdStringThe file ID of the target file or folder.

Return

File — The new shortcut.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)

Creates a shortcut to the provided Drive item ID and resource key, and returns it. A resourcekey is an additional parameter that needs to be passed to access the target file or folder thathas been shared using a link.

// Creates shortcuts for all folders in the user's drive that have a specific// name.// TODO(developer): Replace 'Test-Folder' with a valid folder name in your// drive.constfolders=DriveApp.getFoldersByName('Test-Folder');// Iterates through all folders named 'Test-Folder'.while(folders.hasNext()){constfolder=folders.next();// Creates a shortcut to the provided Drive item ID and resource key, and// returns it.DriveApp.createShortcutForTargetIdAndResourceKey(folder.getId(),folder.getResourceKey(),);}

Parameters

NameTypeDescription
targetIdStringThe ID of the target file or folder.
targetResourceKeyStringThe resource key of the target file or folder.

Return

File — The new shortcut.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

getAccess(email)

Gets the permission granted to a specific user. The method doesn't support returningpermissions for a Google Group or permissions inherited through Google Groups.

Parameters

NameTypeDescription
emailStringThe email address of the user whose permissions should be checked. Google Groups aren't supported.

Return

Permission — The permissions granted to the user.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getAccess(user)

Gets the permission granted to a specific user. The method doesn't support returningpermissions for a Google Group or permissions inherited through Google Groups.

Parameters

NameTypeDescription
userUserA representation of the user whose permissions should be checked.

Return

Permission — The permissions granted to the user.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getDateCreated()

Gets the date theFolder was created.

Return

Date — the date theFolder was created

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getDescription()

Gets the description for theFolder.

Return

String — the description for theFolder

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getEditors()

Gets the list of editors for thisFolder. If the user who executes the scriptdoes not have edit access to theFolder, this method returns an empty array.

//  Gets a folder by its ID.//  TODO(developer): Replace the folder ID with your own.constfolder=DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz');// Gets the list of editors and logs their names to the console.consteditors=folder.getEditors();for(consteditorofeditors){console.log(editor.getName());}

Return

User[] — The list of editors for thisFolder if the user has edit access or an empty array otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFiles()

Gets a collection of all files that are children of the current folder.

Return

FileIterator — A collection of all files that are children of the current folder.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFilesByName(name)

Gets a collection of all files that are children of the current folder and have the given name.

Parameters

NameTypeDescription
nameStringThe name of the files to find.

Return

FileIterator — A collection of all files that are children of the current folder and have the given name.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFilesByType(mimeType)

Gets a collection of all files that are children of the current folder and have the given MIME type.

Parameters

NameTypeDescription
mimeTypeStringThe MIME type of the files to find.

Return

FileIterator — A collection of all files that are children of the current folder and have the given MIME type.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolders()

Gets a collection of all folders that are children of the current folder.

Return

FolderIterator — A collection of all folders that are children of the current folder.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFoldersByName(name)

Gets a collection of all folders that are children of the current folder and have the given name.

Parameters

NameTypeDescription
nameStringThe name of the folders to find.

Return

FolderIterator — A collection of all folders that are children of the current folder and have the given name.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getId()

Gets the ID of theFolder.

Return

String — the ID of theFolder

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getLastUpdated()

Gets the date theFolder was last updated.

Return

Date — the date theFolder was last updated

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getName()

Gets the name of theFolder.

Return

String — the name of theFolder

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getOwner()

Gets the owner of thisFolder.

//  Gets a folder by its ID.//  TODO(developer): Replace the folder ID with your own.constfolder=DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz');// Gets the owner of the folder and logs the name to the console.constfolderOwner=folder.getOwner();console.log(folderOwner.getName());

Return

User — The owner of thisFolder.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getParents()

Gets a collection of folders that are immediate parents of theFolder.

Return

FolderIterator — a collection of folders that are immediate parents of theFolder

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getResourceKey()

Gets the resource key of theFolder that is required to access items thathave been shared using a link.

Return

String — The resource key of theFolder.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getSecurityUpdateEligible()

Gets whether thisFolder is eligible to apply the security update thatrequires a resource key for access when it's shared using a link.

Drive requires a resource key to access some files or folders that have been shared using alink. This change is part of a security update. The update is turned on by default for eligiblefiles and folders. To turn the resource key requirement on or off for eligible files, usesetSecurityUpdateEnabled.

Learn more about theSecurity update for Google Drive.

Return

Boolean — Whether the resource key requirement can be applied for theFolder.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getSecurityUpdateEnabled()

Gets whether thisFolder requires a resource key for access when it'sshared using a link. This requirement is turned on by default for eligible files and folders.To turn the resource key requirement on or off for eligible files, usesetSecurityUpdateEnabled.

Learn more about theSecurity update for Google Drive.

Return

Boolean — Whether the resource key requirement is enabled for thisFolder.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getSharingAccess()

Gets which class of users can access theFolder, besides any individualusers who have been explicitly given access.

Return

Access — which class of users can access theFolder

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getSharingPermission()

Gets the permission granted to those users who can access theFolder,besides any individual users who have been explicitly given access.

Return

Permission — the permissions granted to users who can access theFolder

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getSize()

Gets the number of bytes used to store theFolder in Drive. Note thatGoogle Workspace application files do not count toward Drive storage limits and thus return0 bytes.

Return

Integer — the number of bytes used to store theFolder in Drive

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getUrl()

Gets the URL that can be used to open theFolder in a Google App likeDrive or Docs.

Return

String — the URL that can be used to view thisFolder in a Google App like Drive or Docs

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getViewers()

Gets the list of viewers and commenters for thisFolder. If the user whoexecutes the script does not have edit access to theFolder, this methodreturns an empty array.

//  Gets a folder by its ID.//  TODO(developer): Replace the folder ID with your own.constfolder=DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz');// Gets the list of viewers and logs their names to the console.constviewers=folder.getViewers();for(constviewerofviewers){console.log(viewer.getName());}

Return

User[] — The list of viewers and commenters for thisFolder if the user has edit access or an empty array otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

isShareableByEditors()

Determines whether users with edit permissions to theFolder are allowed toshare with other users or change the permissions.

Return

Booleantrue if users with edit permissions are allowed to share with other users or change the permissions;false if not

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

isStarred()

Determines whether theFolder has been starred in the user's Drive.

Return

Booleantrue if theFolder is starred in the user's Drive;false if not

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

isTrashed()

Determines whether theFolder is in the trash of the user's Drive.

Return

Booleantrue if theFolder is in the trash of the user's Drive;false if not

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

moveTo(destination)

Moves this item to the provided destination folder.

The current user must be the owner of the file or have at least edit access to the item'scurrent parent folder in order to move the item to the destination folder.

Parameters

NameTypeDescription
destinationFolderThe folder that becomes the new parent.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

removeEditor(emailAddress)

Removes the given user from the list of editors for theFolder. This method doesn'tblock users from accessing theFolder if they belong to a class of users who havegeneral access—for example, if theFolder is shared with the user's entiredomain, or if theFolder is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of viewers.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to remove.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

removeEditor(user)

Removes the given user from the list of editors for theFolder. This method doesn'tblock users from accessing theFolder if they belong to a class of users who havegeneral access—for example, if theFolder is shared with the user's entiredomain, or if theFolder is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of viewers.

Parameters

NameTypeDescription
userUserA representation of the user to remove.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

removeViewer(emailAddress)

Removes the given user from the list of viewers and commenters for theFolder. Thismethod has no effect if the user is an editor, not a viewer or commenter. This method alsodoesn't block users from accessing theFolder if they belong to a class of users whohave general access—for example, if theFolder is shared with the user'sentire domain, or if theFolder is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of editors.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to remove.

Return

Folder — ThisFolder for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

removeViewer(user)

Removes the given user from the list of viewers and commenters for theFolder. Thismethod has no effect if the user is an editor, not a viewer. This method also doesn't blockusers from accessing theFolder if they belong to a class of users who have generalaccess—for example, if theFolder is shared with the user's entire domain, orif theFolder is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of editors.

Parameters

NameTypeDescription
userUserA representation of the user to remove.

Return

Folder — ThisFolder for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

revokePermissions(emailAddress)

Revokes the access to theFolder granted to the given user. This method doesn'tblock users from accessing theFolder if they belong to a class of users whohave general access — for example, if theFolder is shared with the user'sentire domain.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user whose access should be revoked.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

revokePermissions(user)

Revokes the access to theFolder granted to the given user. This method doesn'tblock users from accessing theFolder if they belong to a class of users whohave general access — for example, if theFolder is shared with the user'sentire domain.

Parameters

NameTypeDescription
userUserA representation of the user whose access should be revoked.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

searchFiles(params)

Gets a collection of all files that are children of the current folder and match the given searchcriteria. The search criteria are detailed in theGoogle Drive SDK documentation. Note that the Driveservice uses v2 of the Drive API and some query fields differ from v3. Review thefielddifferences between v2 and v3.

Theparams argument is a query string that can contain string values, so take careto escape quotation marks correctly (for example"title contains 'Gulliver\\'sTravels'" or'title contains "Gulliver\'s Travels"').

// Logs the name of every file that are children of the current folder and modified after February 28,// 2022 whose name contains "untitled.""constfiles=DriveApp.getRootFolder().searchFiles('modifiedDate > "2022-02-28" and title contains "untitled"');while(files.hasNext()){constfile=files.next();console.log(file.getName());}

Parameters

NameTypeDescription
paramsStringThe search criteria, as detailed in theGoogle Drive SDK documentation.

Return

FileIterator — A collection of all files that are children of the current folder and match the search criteria.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

searchFolders(params)

Gets a collection of all folders that are children of the current folder and match the given searchcriteria. The search criteria are detailed in theGoogle Drive SDK documentation. Note that the Driveservice uses v2 of the Drive API and some query fields differ from v3. Review thefielddifferences between v2 and v3.

Theparams argument is a query string that can contain string values, so take careto escape quotation marks correctly (for example"title contains 'Gulliver\\'sTravels'" or'title contains "Gulliver\'s Travels"').

// Logs the name of every folder that are children of the current folder and you own and is starred.constfolders=DriveApp.getRootFolder().searchFolders('starred = true and "me" in owners');while(folders.hasNext()){constfolder=folders.next();console.log(folder.getName());}

Parameters

NameTypeDescription
paramsStringThe search criteria, as detailed in theGoogle Drive SDK documentation.

Return

FolderIterator — A collection of all folders that are children of the current folder and match the search criteria.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

setDescription(description)

Sets the description for theFolder.

Parameters

NameTypeDescription
descriptionStringthe new description for theFolder

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setName(name)

Sets the name of theFolder.

Parameters

NameTypeDescription
nameStringthe new name of theFolder

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setOwner(emailAddress)

Changes the owner of theFolder. This method also gives the previous ownerexplicit edit access to theFolder.

Parameters

NameTypeDescription
emailAddressStringthe email address of the user who should become the new owner

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setOwner(user)

Changes the owner of theFolder. This method also gives the previous ownerexplicit edit access to theFolder.

Parameters

NameTypeDescription
userUsera representation of the user who should become the new owner

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setSecurityUpdateEnabled(enabled)

Sets whether theFolder requires a resource key for access when it'sshared using a link. Eligible files and folders are enabled by default.

Learn more about theSecurity update for Google Drive.

Parameters

NameTypeDescription
enabledBooleanWhether to enable the resource key requirement for theFolder.

Return

Folder — ThisFolder, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setShareableByEditors(shareable)

Sets whether users with edit permissions to theFolder are allowed to sharewith other users or change the permissions. The default for a newFolder istrue.

Parameters

NameTypeDescription
shareableBooleantrue if users with edit permissions should be allowed to share with other users or change the permissions;false if not

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setSharing(accessType, permissionType)

Sets which class of users can access theFolder and what permissionsthose users are granted, besides any individual users who have been explicitly given access.

// Creates a folder that anyone on the Internet can read from and write to.// (Domain administrators can prohibit this setting for users of a Google// Workspace domain.)constfolder=DriveApp.createFolder('Shared Folder');folder.setSharing(DriveApp.Access.ANYONE,DriveApp.Permission.EDIT);

Parameters

NameTypeDescription
accessTypeAccesswhich class of users should be able to access theFolder
permissionTypePermissionthe permissions that should be granted to users who can access theFolder

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setStarred(starred)

Sets whether theFolder is starred in the user's Drive. The default fornewFolders isfalse.

Parameters

NameTypeDescription
starredBooleantrue if theFolder should be starred in the user's Drive;false if not

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

setTrashed(trashed)

Sets whether theFolder is in the trash of the user's Drive. Only theowner may trash theFolder. The default for newFolders isfalse.

Parameters

NameTypeDescription
trashedBooleantrue if theFolder should be moved to the trash of the user's Drive;false if not

Return

Folder — thisFolder, for chaining

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

Deprecated methods

addFile(child)

Deprecated. Instead, useFile.moveTo(destination) orcreateShortcut(targetId).

Adds the given file to the current folder. This method does not move the file out of itsexisting parent folder; a file can have more than one parent simultaneously.

Parameters

NameTypeDescription
childFileThe child file to add.

Return

Folder — The new parent of the file added as a child.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

addFolder(child)

Deprecated. Instead, usemoveTo(destination) orcreateShortcut(targetId).

Adds the given folder to the current folder. This method does not move the folder out ofits existing parent folder; a folder can have more than one parent simultaneously.

Parameters

NameTypeDescription
childFolderThe child folder to add.

Return

Folder — The new parent of the folder added as a child.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

removeFile(child)

Deprecated. Instead, useFile.moveTo(destination).

Removes the given file from the current folder. This method does not delete the file, butif a file is removed from all of its parents, it cannot be seen in Drive except by searchingfor it or using the "All items" view.

Parameters

NameTypeDescription
childFileThe child file to remove.

Return

Folder — The previous parent of the child.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

removeFolder(child)

Deprecated. Instead, usemoveTo(destination).

Removes the given folder from the current folder. This method does not delete the folderor its contents, but if a folder is removed from all of its parents, it cannot be seen in Driveexcept by searching for it or using the "All items" view.

Parameters

NameTypeDescription
childFolderThe child folder to remove.

Return

Folder — The previous parent of the child.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/drive

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-12-03 UTC.