I’m working on a Google Apps Script project where I need to locate files in Google Drive based on text patterns rather than exact matches.
Currently I can search for specific text like this:
var results = DriveApp.searchFiles('fullText contains "ABC.123XYZ"')
The problem is that I have many files with similar naming patterns where only part of the text changes. For example, files might contain “ABC.123XYZ”, “ABC.456DEF”, “ABC.789GHI” etc. The part after “ABC.” is always different.
Instead of creating separate search queries for each variation, I want to use a regex pattern like /ABC\.[A-Z0-9]+/ to match any file containing “ABC.” followed by letters and numbers.
How can I implement regex pattern matching with the DriveApp.searchFiles method? The Google Drive API search doesn’t seem to support regex directly, so I’m wondering if there’s a workaround or alternative approach to achieve this kind of pattern-based file searching.
drive api searches are pretty basic tbh. i usually just get all files in a folder first then loop through with regex matching. way faster than multiple api calls and you dont hit quota limits as much. something like DriveApp.getFolderById().getFiles() then test each filename with your pattern.
Unfortunately DriveApp.searchFiles doesn’t support regex patterns directly, but I’ve dealt with this exact scenario in my own projects. What I ended up doing was combining a broader search with client-side filtering using JavaScript regex. First, I search for the common prefix that all your files share, then filter the results programmatically. Something like this:
var files = DriveApp.searchFiles('fullText contains "ABC."');
var pattern = /ABC\.[A-Z0-9]+/;
var matchingFiles = [];
while (files.hasNext()) {
var file = files.next();
if (pattern.test(file.getName()) || pattern.test(file.getDescription())) {
matchingFiles.push(file);
}
}
This approach works well when you have a reasonable number of files to filter through. The initial search narrows down the candidates significantly, then the regex does the precise matching. Keep in mind this searches file names and descriptions rather than full text content, but it’s been reliable for my naming pattern needs.
The Google Drive API search syntax is quite limited when it comes to pattern matching, which can be frustrating. I’ve found that using a two-step approach gives better results than trying to make the initial search too specific. Rather than searching for the exact prefix, I start with a more general query and then apply regex filtering afterwards.
var allFiles = DriveApp.searchFiles('title contains "ABC"');
var regex = /ABC\.[A-Z0-9]+/;
var matches = [];
while (allFiles.hasNext()) {
var file = allFiles.next();
var content = file.getBlob().getDataAsString();
if (regex.test(content)) {
matches.push(file);
}
}
This method actually searches the file content rather than just metadata, which sounds closer to what you need since you mentioned fullText searches. Be aware that getDataAsString() can be slow with large files and has size limitations, so you might want to add some error handling for binary files or very large documents.