0
votes

I want to check if a folder exists AND only if it doesn't exist create a new one. However, that folder could be a subfolder of root or another folder. So how can I do something like mkdirs on Unix where you give it a path and it creates all directories in the path?

BTW - The SDK is a bit frustrating to use as it appears to not have a way to use filesystem paths. Instead, you have to lots of queries and callbacks which is quite messy. W

This is what I use to create a folder:

- (void)createFolder:(NSString *)folderName completion:(void (^)(GTLDriveFile * file, NSError *))handler {
    NSLog(@"createFolder:%@ completion:", folderName);

    GTLDriveFile *folder = [GTLDriveFile object];
    folder.title = folderName;
    folder.mimeType = @"application/vnd.google-apps.folder";

    GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folder uploadParameters:nil];
    [self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLDriveFile *updatedFile, NSError *error) {
        if (error == nil) {
            NSLog(@"Created folder");
            handler(updatedFile, nil);
        } else {
            NSLog(@"An error occurred: %@", error);
            handler(updatedFile, error);
        }
    }];
}
1

1 Answers

0
votes

There is no easy way.

The *nix filesystem is hierarchical, whereas the Google Drive filesystem is not. There are things which the UI and API refers to a "folders", but these are little more than tags/labels.

So if you want a folder hierarchy, you'll need to build it yourself (probably as you've figured out). There are no shortcuts.

You will probably find starting your app by doing a search for all folders

q = "mimeType = 'application/vnd.google-apps.folder'"

is the way to go