How to find folder in google drive

16,386

Solution 1

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'

Solution 2

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'
Share:
16,386
quantm
Author by

quantm

... under construction

Updated on July 06, 2022

Comments

  • quantm
    quantm almost 2 years

    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.

  • Vnge
    Vnge about 11 years
    how do you do this programatically?
  • Claudio Cherubino
    Claudio Cherubino about 11 years
    in Java, use the setQ method
  • Vnge
    Vnge about 11 years
    would it be possible to get a code example or documentation with code examples?
  • IvanRF
    IvanRF over 8 years
    add and trashed = false if you only want untrashed folders
  • Admin
    Admin about 8 years
    If I have same name folder in google drive then it will give two folder ?
  • Mujeeb
    Mujeeb over 2 years
    In my case, even after having the correct code the folder wasn't being listed. Figured out since I was using a service account to upload files (to the folder on Google Drive), I needed to share that folder with the service account too.
  • Gaurav
    Gaurav about 2 years
    Can we crawl one or more folders inside the parent folder? In other words, Can we download the files from multiple parent folders? For example, 2022 Folder > January > 01142022.csv 2022 Folder > February > 02142022.csv I have 2022 (year) as a grandparent and January(month) as a parent folder and 02142022.csv as child file. how to download the files every month which arrives every month. I can only give the folder id of 2022 hard-coded and month-based folder id has to be taken up automatically. Please guide. I want to download all CSV files with their parent and grand parents
  • ChickenFeet
    ChickenFeet about 2 years
    Sorry @Gaurav, comment section is not for asking further questions about a very specific problem you are facing. Of course what you're saying is possible, you just need to know how to code it. Do some reading and write your script step by step, take baby steps and solve each little problem separately. We're not here to do your work for you
  • Mike Johnson Jr
    Mike Johnson Jr about 2 years
    name='foldername' is no longer correct in 2022
  • ChickenFeet
    ChickenFeet about 2 years
    @MikeJohnsonJr care to share the correct syntax?
  • Mike Johnson Jr
    Mike Johnson Jr about 2 years
    title = 'foldername'