Google Drive API: list of files without parent

The files in the Google domain that I administer are in a bad state; There are thousands of files in the root directory. I want to identify these files and move them to a folder under "My Drive".

When I use the API to display the parents for one of these lost files, the result is an empty array. To determine if a file is lost, I can iterate over all the files in my domain and request a list of parents for each. If the list is empty, I know that the file is lost.

But it is terribly slow.

Can I use the Drive API to search for files that don’t have parents?

The "parents" field for the q parameter is apparently not useful for this, since it can only be indicated that the list of parents contains some identifier.

Update:

I am trying to find a quick way to find elements that really are at the root of the document hierarchy. That is, they are brothers and sisters of “My Drive”, and not children of “My Drive”.

+7
source share
4 answers

In Java:

List<File> result = new ArrayList<File>(); Files.List request = drive.files().list(); request.setQ("'root'" + " in parents"); FileList files = null; files = request.execute(); for (com.google.api.services.drive.model.File element : files.getItems()) { System.out.println(element.getTitle()); } 

'root' is the parent folder if the file or folder is in the root directory

+4
source

Rough but simple and it works.

  do { try { FileList files = request.execute(); for (File f : files.getItems()) { if (f.getParents().size() == 0) { System.out.println("Orphan found:\t" + f.getTitle()); orphans.add(f); } } request.setPageToken(files.getNextPageToken()); } catch (IOException e) { System.out.println("An error occurred: " + e); request.setPageToken(null); } } while (request.getPageToken() != null && request.getPageToken().length() > 0); 
+1
source

Try using this in your query:

 'root' in parents 
+1
source
+1
source

All Articles