Although question is already answered, I had a similar situation and wanted to share some code. The code recursively digs through folders and saves the files in the exact hierarchy.
The code is C#, but also pretty much self-explanatory. Hoping it might do some help.
private void downloadFile(DriveService MyService, File FileResource, string path)
{
if (FileResource.MimeType != "application/vnd.google-apps.folder")
{
var stream = new System.IO.MemoryStream();
MyService.Files.Get(FileResource.Id).Download(stream);
System.IO.FileStream file = new System.IO.FileStream(path + @"/" + FileResource.Title, System.IO.FileMode.Create, System.IO.FileAccess.Write);
stream.WriteTo(file);
file.Close();
}
else
{
string NewPath = Path + @"/" + FileResource.Title;
System.IO.Directory.CreateDirectory(NewPath);
var SubFolderItems = RessInFolder(MyService, FileResource.Id);
foreach (var Item in SubFolderItems)
downloadFile(Item, NewPath);
}
}
public List<File> RessInFolder(DriveService service, string folderId)
{
List<File> TList = new List<File>();
var request = service.Children.List(folderId);
do
{
var children = request.Execute();
foreach (ChildReference child in children.Items)
TList.Add(service.Files.Get(child.Id).Execute());
request.PageToken = children.NextPageToken;
} while (!String.IsNullOrEmpty(request.PageToken));
return TList;
}
Note that this code was written for API v2 and Children.List
is not there in API v3. Do use files.list
with ?q='parent_id'+in+parents
if you use v3.