Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a path to a File given it's ID

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)?

like image 383
Brad Tofel Avatar asked Jul 25 '12 19:07

Brad Tofel


People also ask

How do I get the path of a file?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I find the ID of a file?

To locate the File ID, right-click on the name of the file, choose the Get Shareable Link option, and turn on Link Sharing if needed. You will see the link with a combination of numbers and letters at the end, and what you see after `id =` is the File ID.

How do I get the full path of an uploaded file in Python?

To get current file's full path, you can use the os. path. abspath function. If you want only the directory path, you can call os.


1 Answers

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.

like image 187
Alain Avatar answered Oct 01 '22 13:10

Alain