Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active February 22, 2024 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanaikech/e74ead2537b7b3718fc824b6ca60a531 to your computer and use it in GitHub Desktop.
Save tanaikech/e74ead2537b7b3718fc824b6ca60a531 to your computer and use it in GitHub Desktop.
Retrieving Files with Filename Included Special Characters using Google Apps Script

Retrieving Files with Filename Included Special Characters using Google Apps Script

This sample script is for retrieving files with filename included special characters using Google Apps Script. The files are used on Google Drive.

The files with filename of special characters cannot be retrieved using DriveApp.getFilesByName(). This workaround solved this.

As a query parameter, name contains 'filename with special characters' is used. This contains is very important. name='filename with special characters' cannot retrieve such files. Today, it was found that name contains 'filename with special characters' is the workaround. I have been looking for this workaround for a while. Finally, I found this today. By using this method, filename included umlauts can be also retrieved.

In this sample script, it supposes that there is a file with the filename of "ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ".

Script 1 : Use DriveApp

I noticed that DriveApp.searchFiles() can be used for this situation. I think that this is the most simple one.

var files = DriveApp.searchFiles('title contains "ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ" and trashed=false');
while (files.hasNext()) {
  var file = files.next();
  Logger.log("id=%s, name=%s", file.getId(), file.getName())
}

>>> id=#####, name=ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ

If the file of "ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ" is only one on Google Drive, you can use following simple script.

var file = DriveApp.searchFiles('title contains "ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ" and trashed=false').next();

In order to use this script, please enable Drive API at Advanced Google Services for Script 2 and Drive API at API console for Script 2 and Script 3.

In this case, Drive API is v2.

var params = {
  q: "title contains 'ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ' and trashed=false",
  fields: "items(id, title)"
};
var res = Drive.Files.list(params)
Logger.log(res)

Script 3 : Use UrlFetchApp.fetch()

In this case, Drive API is v3.

var filename = "ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ";
var url = "https://www.googleapis.com/drive/v3/files?fields=files(id,name)&q=name+contains+'" + filename + "' and trashed=false";
var params = {
  method: "GET",
  headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
  muteHttpExceptions: true
}
var res = UrlFetchApp.fetch(url, params).getContentText();
Logger.log(res)

Result :

{
 "files": [
  {
   "id": "#####",
   "name": "ÄÖÜÇÃÕÑÆŒØÞÁÉÍÓÚÝÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ"
  }
 ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment