Search for files and folders

You can search for files with the Drive API files: list method. You can call files.list without any parameters, which returns all files on the user's drive. To search for a specific set of files, write a query that includes query terms and operators. Use the query string q, which allows you to specify different operators and terms that you want the query to return.

A basic query contains three parts: a term, an operator and a value for the returned query result. This example shows a basic query:

  `name` `=` 'My document'


  • name is the query term.
  • = operator specifies the condition for the query term.
  • "My document" is the query term's value.

You can modify your searches using the query term operators, which provide conditions for the query terms. To see which operators you can use with each query term, see: Search query terms.

Search for image files using client libraries

The following example shows how to search for JPEG image files and return the list of file names and IDs.

You need to specify the file MIME type, the search location and provide the file metadata you want returned in the file list (e.g. file id, name, and mimeType).

In this example, we use the query parameter to search for files whose mimeType is set to 'image/jpeg' to return only JPEG files. We set spaces to 'drive' to indicate one of three locations to search: 'drive', 'appDataFolder' or 'photos'. We set the pageToken to nextPageToken to continue a previous list request on the next page. No more pages are available to search whennextPageToken returns null.

For more details about the Drive API return values, see files.list reference.


String pageToken = null;
do {
  FileList result = driveService.files().list()
      .setFields("nextPageToken, files(id, name)")
  for (File file : result.getFiles()) {
    System.out.printf("Found file: %s (%s)\n",
        file.getName(), file.getId());
  pageToken = result.getNextPageToken();
} while (pageToken != null);


page_token = None
while True:
    response = drive_service.files().list(q="mimeType='image/jpeg'",
                                          fields='nextPageToken, files(id, name)',
    for file in response.get('files', []):
        # Process change
        print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
    page_token = response.get('nextPageToken', None)
    if page_token is None:


var pageToken = null;
// Using the NPM module 'async'
async.doWhilst(function (callback) {
    q: "mimeType='image/jpeg'",
    fields: 'nextPageToken, files(id, name)',
    spaces: 'drive',
    pageToken: pageToken
  }, function (err, res) {
    if (err) {
      // Handle error
    } else {
      res.files.forEach(function (file) {
        console.log('Found file: ',,;
      pageToken = res.nextPageToken;
}, function () {
  return !!pageToken;
}, function (err) {
  if (err) {
    // Handle error
  } else {
    // All pages fetched

Query search examples

This table shows some basic queries. The actual code will be different depending on the client library you use for your search.

What you want to query Example
Files with the name "hello" name = 'hello'
Files with a name containing the words "hello" and "goodbye" name contains 'hello' and name contains 'goodbye'
Files with a name that does not contain the word "hello" not name contains 'hello'
Folders that are Google apps or have the folder MIME type mimeType = 'application/'
Files that are not folders mimeType != 'application/'
Files that contain the text "important" and in the trash fullText contains 'important' and trashed = true
Files that contain the word "hello" fullText contains 'hello'
Files that do not have the word "hello" not fullText contains 'hello'
Files that contain the exact phrase "hello world" fullText contains '"hello world"'
Files with a query that contains the "" character (e.g., "\authors") fullText contains '\\authors'
Files with ID within a collection, e.g. parents collection '1234567' in parents
Files in an Application data folder in a collection 'appDataFolder' in parents
Files with that users have write permission e.g. user "" '' in writers
Files that user "" has write permission '' in writers
Files that members of the group "" have write permission '' in writers
Files by date modified modifiedTime > '2012-06-04T12:00:00' // default time zone is UTC
Files shared with the authorized user with "hello" in the name sharedWithMe and name contains 'hello'
Files that have not been shared with anyone or domains (only private, or shared with specific users or groups) visibility = 'limited'
Files modified by a specific date that are either images or video modifiedTime > '2012-06-04T12:00:00' and (mimeType contains 'image/' or mimeType contains 'video/')

Search the corpora

Searches that call files: list use user corpus by default. To search other corpora, such as files shared to a G Suite domain, use the corpora parameter.

Multiple corpora may be searched in a single query, though incomplete results may be returned if the combined corpus is too large. If incompleteSearch result is true, not all documents have been searched.

Visszajelzés küldése a következővel kapcsolatban:

Segítségre van szüksége? Keresse fel súgóoldalunkat.