Google is committed to advancing racial equity for Black communities. See how.

Class Folder

Folder

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

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

Methods

MethodReturn typeBrief description
addEditor(emailAddress)FolderAdds the given user to the list of editors for the Folder.
addEditor(user)FolderAdds the given user to the list of editors for the Folder.
addEditors(emailAddresses)FolderAdds the given array of users to the list of editors for the Folder.
addViewer(emailAddress)FolderAdds the given user to the list of viewers for the Folder.
addViewer(user)FolderAdds the given user to the list of viewers for the Folder.
addViewers(emailAddresses)FolderAdds the given array of users to the list of viewers for the Folder.
createFile(blob)FileCreates a file in the current folder from a given Blob 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.
getAccess(email)PermissionGets the permission granted to the given user.
getAccess(user)PermissionGets the permission granted to the given user.
getDateCreated()DateGets the date the Folder was created.
getDescription()StringGets the description for the Folder.
getEditors()User[]Gets the list of editors for this Folder.
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 the Folder.
getLastUpdated()DateGets the date the Folder was last updated.
getName()StringGets the name of the Folder.
getOwner()UserGets the owner of the Folder.
getParents()FolderIteratorGets a collection of folders that are immediate parents of the Folder.
getSharingAccess()AccessGets which class of users can access the Folder, besides any individual users who have been explicitly given access.
getSharingPermission()PermissionGets the permission granted to those users who can access the Folder, besides any individual users who have been explicitly given access.
getSize()IntegerGets the number of bytes used to store the Folder in Drive.
getUrl()StringGets the URL that can be used to open the Folder in a Google App like Drive or Docs.
getViewers()User[]Gets the list of viewers and commenters for this Folder.
isShareableByEditors()BooleanDetermines whether users with edit permissions to the Folder are allowed to share with other users or change the permissions.
isStarred()BooleanDetermines whether the Folder has been starred in the user's Drive.
isTrashed()BooleanDetermines whether the Folder 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 the Folder.
removeEditor(user)FolderRemoves the given user from the list of editors for the Folder.
removeViewer(emailAddress)FolderRemoves the given user from the list of viewers and commenters for the Folder.
removeViewer(user)FolderRemoves the given user from the list of viewers and commenters for the Folder.
revokePermissions(user)FolderRevokes the access to the Folder granted to the given user.
revokePermissions(user)FolderRevokes the access to the Folder granted to the given user.
searchFiles(params)FileIteratorGets a collection of all files that are children of the current folder and match the given search criteria.
searchFolders(params)FolderIteratorGets a collection of all folders that are children of the current folder and match the given search criteria.
setDescription(description)FolderSets the description for the Folder.
setName(name)FolderSets the name of the Folder.
setOwner(emailAddress)FolderChanges the owner of the Folder.
setOwner(user)FolderChanges the owner of the Folder.
setShareableByEditors(shareable)FolderSets whether users with edit permissions to the Folder are allowed to share with other users or change the permissions.
setSharing(accessType, permissionType)FolderSets which class of users can access the Folder and what permissions those users are granted, besides any individual users who have been explicitly given access.
setStarred(starred)FolderSets whether the Folder is starred in the user's Drive.
setTrashed(trashed)FolderSets whether the Folder is in the trash of the user's Drive.

Detailed documentation

addEditor(emailAddress)

Adds the given user to the list of editors for the Folder. If the user was already on 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 — This Folder, for chaining.

Authorization

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

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

addEditor(user)

Adds the given user to the list of editors for the Folder. If the user was already on 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 — This Folder, for chaining.

Authorization

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

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

addEditors(emailAddresses)

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

Parameters

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

Return

Folder — This Folder, for chaining.

Authorization

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

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

addViewer(emailAddress)

Adds the given user to the list of viewers for the Folder. If the user was already on the list of editors, this method has no effect.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

Folder — This Folder, for chaining.

Authorization

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

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

addViewer(user)

Adds the given user to the list of viewers for the Folder. If the user was already on the list of editors, this method has no effect.

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

Folder — This Folder, for chaining.

Authorization

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

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

addViewers(emailAddresses)

Adds the given array of users to the list of viewers for the Folder. If any of the users 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 — This Folder, for chaining.

Authorization

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

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

createFile(blob)

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

// Create an image file in Google Drive using the Maps service.
var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
DriveApp.getRootFolder().createFile(blob);

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 following scopes:

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

createFile(name, content)

Creates a text file in the current folder with the given name and contents. Throws an exception if content 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 following scopes:

  • 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. Throws an exception if content 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 following scopes:

  • 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 following scopes:

  • 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 following scopes:

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

getAccess(email)

Gets the permission granted to the given user.

Parameters

NameTypeDescription
emailStringthe email address 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 following scopes:

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

getAccess(user)

Gets the permission granted to the given user.

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 following scopes:

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

getDateCreated()

Gets the date the Folder was created.

Return

Date — the date the Folder was created

Authorization

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

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

getDescription()

Gets the description for the Folder.

Return

String — the description for the Folder

Authorization

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

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

getEditors()

Gets the list of editors for this Folder. If the user who executes the script does not have edit access to the Folder, this method returns an empty array.

Return

User[] — An array of users with edit permission.

Authorization

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

  • 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 following scopes:

  • 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 following scopes:

  • 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 following scopes:

  • 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 following scopes:

  • 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 following scopes:

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

getId()

Gets the ID of the Folder.

Return

String — the ID of the Folder

Authorization

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

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

getLastUpdated()

Gets the date the Folder was last updated.

Return

Date — the date the Folder was last updated

Authorization

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

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

getName()

Gets the name of the Folder.

Return

String — the name of the Folder

Authorization

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

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

getOwner()

Gets the owner of the Folder.

Return

User — a User object representing the owner

Authorization

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

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

getParents()

Gets a collection of folders that are immediate parents of the Folder.

Return

FolderIterator — a collection of folders that are immediate parents of the Folder

Authorization

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

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

getSharingAccess()

Gets which class of users can access the Folder, besides any individual users who have been explicitly given access.

Return

Access — which class of users can access the Folder

Authorization

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

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

getSharingPermission()

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

Return

Permission — the permissions granted to users who can access the Folder

Authorization

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

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

getSize()

Gets the number of bytes used to store the Folder in Drive. Note that G Suite application files do not count toward Drive storage limits and thus return 0 bytes.

Return

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

Authorization

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

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

getUrl()

Gets the URL that can be used to open the Folder in a Google App like Drive or Docs.

Return

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

Authorization

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

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

getViewers()

Gets the list of viewers and commenters for this Folder. If the user who executes the script does not have edit access to the Folder, this method returns an empty array.

Return

User[] — An array of users with view or comment permission.

Authorization

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

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

isShareableByEditors()

Determines whether users with edit permissions to the Folder are allowed to share 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 following scopes:

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

isStarred()

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

Return

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

Authorization

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

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

isTrashed()

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

Return

Booleantrue if the Folder 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 following scopes:

  • 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's current parent folder in order to move the item to the destination folder.

Parameters

NameTypeDescription
destinationFolderThe folder that becomes the new parent.

Return

Folder — This Folder, for chaining.

Authorization

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

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

removeEditor(emailAddress)

Removes the given user from the list of editors for the Folder. This method doesn't block users from accessing the Folder if they belong to a class of users who have general access—for example, if the Folder is shared with the user's entire domain, or if the Folder 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 — This Folder, for chaining.

Authorization

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

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

removeEditor(user)

Removes the given user from the list of editors for the Folder. This method doesn't block users from accessing the Folder if they belong to a class of users who have general access—for example, if the Folder is shared with the user's entire domain, or if the Folder 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 — This Folder, for chaining.

Authorization

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

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

removeViewer(emailAddress)

Removes the given user from the list of viewers and commenters for the Folder. This method has no effect if the user is an editor, not a viewer or commenter. This method also doesn't block users from accessing the Folder if they belong to a class of users who have general access—for example, if the Folder is shared with the user's entire domain, or if the Folder 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 — This Folder for chaining.

Authorization

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

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

removeViewer(user)

Removes the given user from the list of viewers and commenters for the Folder. This method has no effect if the user is an editor, not a viewer. This method also doesn't block users from accessing the Folder if they belong to a class of users who have general access—for example, if the Folder is shared with the user's entire domain, or if the Folder 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 — This Folder for chaining.

Authorization

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

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

revokePermissions(user)

Revokes the access to the Folder granted to the given user. This method does not block users from accessing the Folder if they belong to a class of users who have general access — for example, if the Folder is shared with the user's entire domain.

Parameters

NameTypeDescription
userStringthe email address of the user whose access should be revoked

Return

Folder — this Folder, for chaining

Authorization

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

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

revokePermissions(user)

Revokes the access to the Folder granted to the given user. This method does not block users from accessing the Folder if they belong to a class of users who have general access — for example, if the Folder is shared with the user's entire domain.

Parameters

NameTypeDescription
userUsera representation of the user whose access should be revoked

Return

Folder — this Folder, for chaining

Authorization

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

  • 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 search criteria. The search criteria are detailed in the Google Drive SDK documentation. Note that the params argument is a query string that may contain string values, so take care to escape quotation marks correctly (for example "title contains 'Gulliver\\'s Travels'" or 'title contains "Gulliver\'s Travels"').

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

Parameters

NameTypeDescription
paramsStringThe search criteria, as detailed in the Google 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 following scopes:

  • 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 search criteria. The search criteria are detailed in the Google Drive SDK documentation. Note that the params argument is a query string that may contain string values, so take care to escape quotation marks correctly (for example "title contains 'Gulliver\\'s Travels'" or 'title contains "Gulliver\'s Travels"').

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

Parameters

NameTypeDescription
paramsStringThe search criteria, as detailed in the Google 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 following scopes:

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

setDescription(description)

Sets the description for the Folder.

Parameters

NameTypeDescription
descriptionStringthe new description for the Folder

Return

Folder — this Folder, for chaining

Authorization

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

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

setName(name)

Sets the name of the Folder.

Parameters

NameTypeDescription
nameStringthe new name of the Folder

Return

Folder — this Folder, for chaining

Authorization

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

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

setOwner(emailAddress)

Changes the owner of the Folder. This method also gives the previous owner explicit edit access to the Folder.

Parameters

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

Return

Folder — this Folder, for chaining

Authorization

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

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

setOwner(user)

Changes the owner of the Folder. This method also gives the previous owner explicit edit access to the Folder.

Parameters

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

Return

Folder — this Folder, for chaining

Authorization

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

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

setShareableByEditors(shareable)

Sets whether users with edit permissions to the Folder are allowed to share with other users or change the permissions. The default for a new Folder is true.

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 — this Folder, for chaining

Authorization

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

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

setSharing(accessType, permissionType)

Sets which class of users can access the Folder and what permissions those 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 G Suite domain.)
var folder = DriveApp.createFolder('Shared Folder');
folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);

Parameters

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

Return

Folder — this Folder, for chaining

Authorization

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

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

setStarred(starred)

Sets whether the Folder is starred in the user's Drive. The default for new Folders is false.

Parameters

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

Return

Folder — this Folder, for chaining

Authorization

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

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

setTrashed(trashed)

Sets whether the Folder is in the trash of the user's Drive. Only the owner may trash the Folder. The default for new Folders is false.

Parameters

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

Return

Folder — this Folder, for chaining

Authorization

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

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

Deprecated methods