I often need to find a bunch of files in a particular folder, and if I also need to get files in subfolders also, it can be a little longwinded. I prefer to deal with an array of matching files rather than mess around with file iterators, and I also prefer to deal with folder paths (such as /books/going gas/assets) rather than with folder ids.

This utility is available from the cUseful library, and will generate an array of matching files, taking care of recursing into folder structures as well as allowing you to specify folder paths instead of folder ids if you prefer.

Using DriveUtils

Since the cUseful library is dependency free, you need to pass it your DriveApp and provoke a drive authorization the first time you run your script. If youa re calling any functions from the DriveUtils namespace of cUseful, your script should always start with this line (including the comment).

By making cUseful dependency free like this, it can be included in scripts without generating unnecessary requests for authorization. If you are building a multipurpose library, this is a good approach to keep authorization requests relevant to users of your library.

How to use

Let’s say I want all files in the folder under a path on Drive that starts with “/Published slides”.

More likely though I’ll want a specific type of file – in this case presentations – so I’ll add the mimetype for presentations

If I want the files in all the subfolders too, I can do this

The result

Here’s what the resultant pile of files looks like. The file and folder properties are DriveApp File and Folder objects respectively.

Using the folder id

If you prefer to use the folder id rather than the path, just supply it instead of a path, as below.
var files = driveUtils.getPileOfFiles (“0B92ExLh4POiZR1pFTlBaREFfcWs”,”application/vnd.google-apps.presentation”, true);

The returned result will be the same regardless of whether you provided an id or a path.

The code

You’ll need the cUseful library (key below), or you can pull it from github to create your own copy.

Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j

A quick visual test is this webapp, which I’ve used to enumerate my drive.


The code is below and on github.

index.html

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.