0

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?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • you cant really use the path you need to find the parent id. Files have parent ids the parent id is the fileid of the folder the name will be the name of the folder. You should try using the q parameter so that you can search in each directory. – Linda Lawton - DaImTo Aug 10 '20 at 09:12
  • Thanks I will check this but if I find the folder how can I list the file within it? – Joan Venge Aug 10 '20 at 09:13
  • Do a file.list with Q = "mimeType = 'application/vnd.google-apps.folder', name contains 'A'"} will get you back the fileid of the folder you want. Then you can do another file.list where parent equal to the id that you got from the first request – Linda Lawton - DaImTo Aug 10 '20 at 10:02
  • Relevant information on Drive and file paths: https://stackoverflow.com/questions/62092499 – Rafa Guillermo Aug 10 '20 at 13:10

1 Answers1

0

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

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks I tried it, but the first code you sent throws an error: Google.GoogleApiException HResult=0x80131500 Message=Google.Apis.Requests.RequestError Invalid Value [400] Errors [ Message[Invalid Value] Location[q - parameter] Reason[invalid] Domain[global] ] I have the folder at the top level in Google Drive. This example works for the first part but is different than the way you are searching, but I think it's also from your website. – Joan Venge Aug 10 '20 at 10:38
  • Its probably because i didn't actually test that code i typed it up from memory. Try adding **and** between the two items in the search parm – Linda Lawton - DaImTo Aug 10 '20 at 10:42
  • Thanks a lot appreciate it, I will also try in the mean time, coz the other one from the link I posted works: https://www.daimto.com/search-files-on-google-drive-with-c/ Except I only find the top folder, the second query to find the folder B fails with the same error. – Joan Venge Aug 10 '20 at 10:44
  • **mimeType = 'application/vnd.google-apps.folder' and name contains 'A'** should give you the parent id of A – Linda Lawton - DaImTo Aug 10 '20 at 10:52