Google Drive API: find a file by name using wildcards?

Viewed 1868

When using the Google Drive API v3, can one search for a file by its name using wildcards or regular expressions? The docs don't mention anything.

I am trying to match a set of files whose names have the format

backup_YYYY-MM-DD-XXXX_NameOfWebsite_xxxxxxxxxx.zip

And am wondering what's the best way to construct a pattern that might match it. Of course, I could follow the docs and just do something like:

q="name contains 'backup' and name contains 'NameOfWebsite'"

But if I need to match a different pattern, or something with more than 2 distinctive strings in its filename ("backup_" and "NameOfWebsite"), you can quickly see what a pain would be to construct a query in that way:

q="name contains 'string1' and name contains 'string2' and name contains...
1 Answers

Answer:

You can't use a wildcard in the middle of a file name when making a Drive.list request with a q parameter.

More Information:

The name field only takes three operators - =, != and contains:

  • The = operator is regular equivalence and with this you can not use a wildcard.
  • name = 'backup*' will return no results.
  • The != operator is not equivalence, not relevant here but does the opposite of =
  • The contains operator. You can use wildcards with this, but with restrictions:
  • name contains 'backup*' will return all files with filenames starting with the string backup.
  • name contains '*NameOfWebsite' will return all files with filenames that have a word starting with the string NameOfWebsite. The file name backup0194364-NameOfWebsite.zip will not be returned, because there is no space before the string.

Therefore, the only way for this to work is if you do it the way you have already started to realise; string chaining:

name contains 'backup' and name contains 'NameOfWebsite' and name contains ...

References:

Related