Listing Folders and Files Using the GoogleDrive Component
In Google Drive, every file and folder is a resource with a unique ID, and a resource's location is determined by the IDs of the folders that contain it (its parents). The GoogleDrive component in Cloud Storage provides the ListChildren method to list the resources within one or more folders, and the ListResourcesMode configuration setting to limit the results to only files or only folders. The examples below show several common ways to list and navigate the folder structure of a drive.
Contents
- Listing the Subfolders of a Folder
- Listing the Files in a Folder
- Finding a Folder by Name
- Walking the Entire Folder Structure
- Using a Custom Search Query
Listing the Subfolders of a Folder
The ListChildren method lists the resources within the specified folder, and populates the Children collection. The special ID root may be used to refer to the root folder of the drive. To list only folders, set the ListResourcesMode configuration setting to 2 before calling the method.
If there are more results than fit in a single page, the ChildMarker property will be populated. Continue calling ListChildren until ChildMarker is empty to accumulate all pages of results in the Children collection.
// List only folders (0 = files and folders, 1 = files only, 2 = folders only).
googledrive.Config("ListResourcesMode=2");
// List the subfolders of the root folder.
do {
googledrive.ListChildren("root");
} while (!string.IsNullOrEmpty(googledrive.ChildMarker));
foreach (GDriveResource folder in googledrive.Children) {
Console.WriteLine(folder.Name + " (" + folder.Id + ")");
}
To list the subfolders of a specific folder, pass the folder's ID instead of root. Multiple folder IDs may also be passed as a comma-separated list to list the children of several folders at once.
string folderId = "FOLDER_ID";
googledrive.Config("ListResourcesMode=2");
do {
googledrive.ListChildren(folderId);
} while (!string.IsNullOrEmpty(googledrive.ChildMarker));
Listing the Files in a Folder
To list only the files within a folder (excluding any subfolders), set the ListResourcesMode configuration setting to 1.
// List only files.
googledrive.Config("ListResourcesMode=1");
do {
googledrive.ListChildren("root");
} while (!string.IsNullOrEmpty(googledrive.ChildMarker));
foreach (GDriveResource file in googledrive.Children) {
// Skip any files that have been moved to the trash.
if (file.Trashed) continue;
Console.WriteLine(file.Name + " (" + file.Size + " bytes)");
}
Note that items in the trash may be included in the results. The Trashed field of each resource indicates whether it is in the trash.
Finding a Folder by Name
Since most methods reference folders by their ID, a common first step is to look up the ID of a folder by its name. This can be done by listing the subfolders of the parent folder and comparing their names. Note that Google Drive allows multiple folders with the same name to exist within the same parent folder, so the first match may not be the only one.
// Find the ID of the "Reports" folder within the root folder.
string reportsId = "";
googledrive.Config("ListResourcesMode=2");
do {
googledrive.ListChildren("root");
} while (!string.IsNullOrEmpty(googledrive.ChildMarker));
foreach (GDriveResource folder in googledrive.Children) {
if (folder.Name == "Reports" && !folder.Trashed) {
reportsId = folder.Id;
break;
}
}
Walking the Entire Folder Structure
To process every folder and file in a drive, list the children of each folder, and then repeat the process for each subfolder. Since each call to ListChildren for a new folder clears the Children collection, the results for a folder should be copied before its subfolders are listed.
void PrintFolder(GoogleDrive googledrive, string folderId, string indent) {
// List both files and folders.
googledrive.Config("ListResourcesMode=0");
do {
googledrive.ListChildren(folderId);
} while (!string.IsNullOrEmpty(googledrive.ChildMarker));
// Copy the results, since listing a subfolder will replace the contents of the Children collection.
List<GDriveResource> children = new List<GDriveResource>();
foreach (GDriveResource child in googledrive.Children) {
if (!child.Trashed) children.Add(child);
}
foreach (GDriveResource child in children) {
Console.WriteLine(indent + child.Name);
if (child.ResourceType == GDriveResourceTypes.gdrtFolder) {
PrintFolder(googledrive, child.Id, indent + " ");
}
}
}
// Print the folder structure of the entire drive, starting at the root folder.
PrintFolder(googledrive, "root", "");
Using a Custom Search Query
For more complex queries, a Google Drive search query can be supplied by adding a query parameter named q with the AddQueryParam method before calling ListResources. The results are placed in the Resources collection, and paging works the same way as described above, using the ResourceMarker property. Refer to Google's documentation on searching for files and folders for details on the query syntax.
// Include both files and folders, so that the component does not add its own filter to the query.
googledrive.Config("ListResourcesMode=0");
// List the PDF files in a specific folder whose names contain "invoice".
googledrive.AddQueryParam("q", "'FOLDER_ID' in parents and mimeType = 'application/pdf' and name contains 'invoice' and trashed = false");
do {
googledrive.ListResources();
} while (!string.IsNullOrEmpty(googledrive.ResourceMarker));
foreach (GDriveResource file in googledrive.Resources) {
Console.WriteLine(file.Name + " (" + file.Id + ")");
}
// Remove the query parameter so that it does not affect later requests.
googledrive.QueryParams.Clear();
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.