简体   繁体   中英

How to find files inside a specific folder in Google Drive v3 API using C#?

I have a top parent folder A, then inside this another folder called B, then I have other folders inside these using numbers 001, 002, 003, etc. Inside each I have an image.

All I am trying to do is to find all the images in order using an absolute path. So:

folderpath = "/A/B/"
foreach folder inside folderpath:
    //folder is going to be 001, 002, 003
    files = folder.listfiles()
    if (files.Count == 1)
        Console.WriteLine(files[0].Name);

How can I do this?

Basicly you are looking at two requests. The first request to find the directory file id (parent id). The second request to find the files within that directory.

Get the folder with the name of A:

var fileListRequest = service.Files.List();
fileListRequest.Q = "mimeType = 'application/vnd.google-apps.folder' and name contains 'A'";
var fileListResponse = fileListRequest.Execute();

Get all the files with a parent id of the previous request.

var filesInParent = service.Files.List();
filesInParent.Q = $"parents in {fileListResponse.Files.FirstOrDefault().Id}";
var allFilesInParentResponse = filesInParent.Execute();

Issues will arise with this if you have more then one directory with the name of A you will probably need to check its parent directory in that case to ensure that you have the proper directory that you are looking for.

Just trick is to check for mimeType = 'application/vnd.google-apps.folder' that means that its a folder.

documentation on search

Some background info on search can be found here

List all files on Google drive

An alternate solution would be to just list all of the files on your google drive and sort though it locally. Due to the nature of the page streamer i would not recomend this if you have a lot of files on your drive account as each fetch is going to eat one of your quota But if your interested i have a tutorial on how to do it. List All files on Google drive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM