Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Drive API with Java, how to list only files in the Drive Section/Folder

I am trying to get all files that are in a specific section of Google Drive (The Drive Section).

Those are the files I am looking for:

enter image description here

You can see a folder named Projet 3 and a file named Processus TP2 questions. In the folder I only have one file.

But when I try to list all the files, I get thousands of files. If I search for them using the search bar on top of Google Drive, it finds them, but I have no idea where they are (maybe Gmail attachement?).

This is my search query:

FileList result = service.files().list()
    
    .setQ("mimeType != 'application/vnd.google-apps.folder' 
            and trashed = false")
    
    .setSpaces("drive")
    .setFields("nextPageToken, files(id, name, parents)")
    .setPageToken(pageToken)
    
    .execute();

How can I list only the files I can see in my Drive Section/Folder on the web UI?

Thank you very much.

like image 583
David Gourde Avatar asked May 01 '16 17:05

David Gourde


People also ask

How do I get a list of files in Google Drive folder?

Create a new Google Sheets spreadsheet or open an existing spreadsheet where you want the list saved. Here's a tip: You can quickly create a new Google Sheets spreadsheet using https://spreadsheet.new. Create a sheet in the spreadsheet called "Files". The list of files will be written to the sheet.

Can Google Drive tell me how many files are in a folder?

3. Right-click on any one file and select “Share…” 4. On the following screen you should be able to see the number of files you have in the folder in the brackets next to the words “Share with others.”


1 Answers

Use "parents" property in your query, like this:

FileList result = service.files().list()
    
.setQ("'root' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false")
    
.setSpaces("drive")
.setFields("nextPageToken, files(id, name, parents)")
.setPageToken(pageToken)
    
.execute();

If you want to list the contents of a specific folder rather than root - use that folder's id instead of 'root' in the query.

like image 133
Alexander Kravets Avatar answered Nov 14 '22 22:11

Alexander Kravets