quantm
quantm

Reputation: 244

How to find folder in google drive

I have implement the function upload file to google drive. I have done it following the guide in quick start.

Now i'm doing the function upload file to specific folder *(Ex: upload file to "My_Folder_name")*. I found the sample in API Reference. The trouble is how can i get the parentId (folder id) if i only know the name of folder.

I could not find any function to search folder by name...

Is the only one way to get folder by get all the files, then check MimeType is equals with "application/vnd.google-apps.folder" and compare it with folder name.

if("application/vnd.google-apps.folder".equals(body.getMimeType()) && "My_Folder_name".equals(body.getTitle()) ){
            ....        
}

Thanks in advance.

Upvotes: 11

Views: 21536

Answers (5)

SirPhemmiey
SirPhemmiey

Reputation: 641

Just in case you're looking for a Typescript/Javascript answer here's it.

const fileList = await this.getInstance().files.list({
        q: `(mimeType='application/vnd.google-apps.folder' and name='${folderName}') and trashed = false`,
        fields: "nextPageToken, files(id, name)",
        supportsAllDrives: true,
        spaces: 'drive',
        orderBy: 'name desc'
      });

if you want to search within another folder then you can do

const fileList = await this.getInstance().files.list({
        q: `"${parentFolderId}" in parents and (mimeType='application/vnd.google-apps.folder' and name='${folderName}') and trashed = false`,
        fields: "nextPageToken, files(id, name)",
        supportsAllDrives: true,
        spaces: 'drive',
        orderBy: 'name desc'
      });

Of course, you need to modify a little bit for your usecase. Especially how you get your Drive Instance

Upvotes: 1

ChickenFeet
ChickenFeet

Reputation: 2813

Edit as per Mike's comment:

name='foldername' is no longer correct in 2022

The new syntax should be: mimeType='application/vnd.google-apps.folder' and title = 'foldername'


Original answer:

Query should be as follows:

mimeType='application/vnd.google-apps.folder' and name='foldername'

In Python:

service.files().list(q="mimeType='application/vnd.google-apps.folder' and name='foldername'",
                                         spaces='drive',
                                         fields='nextPageToken, files(id, name)',
                                         pageToken=page_token).execute()

Note: name='foldername' not title='foldername'

Upvotes: 15

Manu Singh
Manu Singh

Reputation: 39

I think this will help you

/*String pageToken = null;
            do {
                FileList fileList = driveService.files().list().setQ("'" + parentFolderId + "' in parents")
                        .setFields("nextPageToken, files(id,parents,name,mimeType)")
                        .setPageToken(pageToken).execute();

                if (fileList.getFiles().size() == 0)
                    System.out.println("No data inside the folder");

                    for (File file : fileList.getFiles()) {
                        System.out.println(file.getId());
                    }
                pageToken = fileList.getNextPageToken();
            } while (pageToken != null);*/

Upvotes: 0

user3198265
user3198265

Reputation: 56

You need to do two things:

Use files.list (which gives full file resources)(https://developers.google.com/drive/v2/reference/files/list). Amend your query to add the parent id you want to search in.

mimeType = 'application/vnd.google-apps.folder' and 'folderId' in parents

Upvotes: 0

Claudio Cherubino
Claudio Cherubino

Reputation: 15004

You can add the q query parameter to your request URL and use the Drive query language to restrict your search:

https://developers.google.com/drive/search-parameters

To search for folders only and restrict the search by title, your query should look like:

mimeType = 'application/vnd.google-apps.folder' and title = 'My_Folder_name'

Upvotes: 13

Related Questions