Finding a path to a File given it's ID
-
11-12-2019 - |
Question
I get a FileList with:
String q = "title contains '"+query+"' and trashed = false";
FileList list = drive.files().list().setQ(q).execute();
I want to find a path to each matching file in the FileList.
I understand that the same File can appear in multiple Folders.
From what I've found, the only way to create a path for a File is to repeatedly call:
drive.files().get(id).execute()
and then choose an ID from that File's parents list, walking up the tree until the root is reached.
While navigating the tree, I could just choose the first parent, or do a "BFS" until I find the root.
Is this really the only way to find a path to a File, or have I missed some part of the API?
If this is the only way, can Folders have multiple parents, too, making cycles possible?
(/a/b/c => /a/b/c/b/c/b/c... if b has parents a & c)?
Solution
Since folders (which are simply special Drive File with a specific MIME type) can have multiple parents, I would recommend the BFS approach and making sure you handle loops.
For the stop condition, you can save the root folder's ID that you can retrieve from the about.rootFolderId attribute.