I would like to get a list of files and folders in the root directory without having to sort through all the files. Is there a query that would do this?
4 Answers
The root folder can also be addressed with a special alias named "root", so you can get all files and folders in the root with the following query:
https://www.googleapis.com/drive/v2/files?q='root' in parents
Remember to escape the URL if not using one of the client libraries (they automatically take care of it).
For more details about the search query language, check https://developers.google.com/drive/search-parameters

- 14,896
- 1
- 35
- 42
-
Thanks works like a charm! But I found a few files that seem to not have any parent, they don't come up in the root query or any other folder query. Is that possible? Bug? – PizzaPanther Jul 30 '12 at 14:42
-
That's a great one Claudio! Did I miss this in the documentation? – Brad Tofel Jul 30 '12 at 16:47
-
i am using the service account with the php library. how to i access all files and folders? – Jayapal Chandran Feb 11 '13 at 20:19
-
You can use domain-wide delegation to access other domain users' documents: https://developers.google.com/drive/delegation – Claudio Cherubino Feb 11 '13 at 20:42
-
so thanks i'am looking for than almost two hours there is no any document for explain this – Toprak Jun 30 '16 at 19:44
-
escape like this \'root\' in parents" c# – Kashif Hanif Sep 14 '17 at 15:44
-
is there a way to search in multiple folders? not only root. – aleXela Sep 26 '17 at 21:56
This code will display all files and folder of your ROOT DIRECTORY. just copy and paste this code and you will get all your root's file and folder.
List<File> result = new ArrayList<File>();
Files.List request = null;
try {
request = mService.files().list();
FileList files = request.setQ("'root' in parents and trashed=false").execute();
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
}
catch (IOException e)
{
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
//Print out all the files and folder of root Directory
for(File f:result)
{
System.out.println("recvd data are: "+f.getTitle());
}

- 10,505
- 1
- 82
- 81
-
2you might want to add trashed=false to prevent deleted files showing up – pinoyyid Apr 03 '14 at 17:06
-
@pinoyyid thanks for your suggestion. I have updated it. You can see it here now. – Pir Fahim Shah Apr 04 '14 at 06:37
-
Google drive v3 Reference the documentation helps to create DriveClient object.
Run this below method in the background thread(Android).
Note: Required Scope permission "DriveScopes.DRIVE"
protected String[] getListChildren(String parentId) throws Exception {
String[] children = null;
parentId = parentId == null ? "root" : parentId;
String fileQuery = "'" + parentId + "' in parents and trashed=false";
FileList files = driveService.files().list().setQ(fileQuery).execute();
List<String> fileNames = new ArrayList<String>();
for (File file : files.getFiles()) {
fileNames.add(file.getName());
}
children = fileNames.toArray(new String[0]);
return children;
}

- 909
- 11
- 12
The query for a Shared Drive should be "'driveId' in parents
.
Using 'root'
works for My Drive only.

- 3,589
- 3
- 27
- 30