8
8
8x8z2017-08-12 20:40:29
Google Apps Script
8x8z, 2017-08-12 20:40:29

The getFilesByName() method does not find files if the Cyrillic name contains uppercase letters?

In general, it all started with the fact that it was necessary to save attached files received via GmailApp to Google Drive.
Everything went well until it became necessary to overwrite the file if it already exists on the disk.
If you just save, then files with the same name are saved as FileName(n-1).ext - where n is the number of received files with the same name.
Accordingly, it became necessary to determine whether a file with a certain name is present on the disk and, if the result is positive, delete it and save the new one.
And that's what was found when searching for files with Cyrillic.

function startFindFile() {
          
  var folder =  DriveApp.getFolderById(DriveApp.getFoldersByName("prices").next().getId()); 
  var fileNameCyr1 = "Прайс1_Склад ЦС.xls";
  var fileNameCyr2 = "прайс1_склад цс.xls";
  var fileNameEng = "Price.txt";
  
  Logger.log("List all files:" );
  var files = folder.getFiles();
  findFile(files);
  
  Logger.log("Find files: " + fileNameEng ); 
  files = folder.getFilesByName(fileNameEng);
  findFile(files);
  
  Logger.log("Find files: " + fileNameCyr1 ); 
  files = folder.getFilesByName(fileNameCyr1);
  findFile(files);
  
  Logger.log("Find files: " + fileNameCyr2 );
  files = folder.getFilesByName(fileNameCyr2);
  findFile(files);
}

function findFile(files) {

   while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName() + " " + file.getId());

   }

}

What do we get as a result.
getFilesByName looks for Cyrillic files in lower case only?
40f8a0ff3d264aaf9c6b233bf9105646.jpgQuestion: how to find a file on Google Drive in the name of which there are Cyrillic letters in upper case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2017-08-13
@8x8z

I'm reporting this bug right now. Put an asterisk here .
Use a less precise search with additional processing of the results. Both the DriveApp service and the Drive service can handle the title contains. See Search for Files . At the moment, 2017-08-13, API v2 is used, and for searching in this case, this is fundamental. The following code will return what you expect:

function getFileByName(name) { 
  var files = DriveApp.searchFiles(Utilities.formatString('title contains "%s" and trashed=false', name));
  var result = undefined;
  while(files.hasNext()){
    var file = files.next();
    if(file.getName() === name){
      result = file;
      break;
    }
  }
  return result;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question