0

I have a script to rename files in a Google Drive but it is not working. In particular, I want to erase the last character of the file name and set this new name as the file name.

function re_name() {
  var folder = DriveApp.getFolderById("folderId");
  var files =  folder.getFiles();
  var date = new Date("August 29, 2018");
  while(files.hasNext()){
    var aux = files.next();
    if(aux.getDateCreated() == date ){
      aux.setName(aux.getName().replace(".s", ""))
    }
  }

Also, I'm not sure if replace() that exists in java can be used in Google Script.

But even if it is not the case to use replace() I can't seem to make modifications an follows:

aux.setName(aux.getName() + ".f")

Please help to find the mistake or else I would very much appreciate an alternative method.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • 1
    Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – TheMaster Jan 17 '19 at 17:55

1 Answers1

0

Try this:

function re_name() {
  var folder=DriveApp.getFolderById("folderId");
  var files=folder.getFiles();
  var date=new Date("August 29, 2018");
  while(files.hasNext()){
   var aux=files.next();
   if(aux.getDateCreated() == date){
     aux.setName(aux.getName().slice(0,-1));//remove last character
     //aux.setName(aux.getName().replace(/s$/,'');//replace last s
     //aux.setName(aux.getName().replace(/\w$/,'');//replace last word character[A-Za-z0-9_]
   }
 }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54