Google App Script – Change File Name Automation

google-apps-scriptgoogle-drive

I am working on a script in App Script that will change the file name of all the files in a certain folder based on their creation date using this format yyyy_MM_dd_filename. I was able to create the script and it is working when I run it however everytime I run it again it appends another date on the file name which makes it duplicated.

What I wanted to happen is that when I run the script with a new file it will not change the file name of those who have already the date format that I created but instead just changed those that was just saved and newly uploaded in Google Drive.

This is my code:

function changeName() {
      
      var folder_access = DriveApp.getFolderById("1qSQ-K2R97PIfDHztmU0iwM0y_GxPk2zG");
      var folder_files = folder_access.getFiles();
            
      var file_name = folder_files.next();
      var filename_format = Utilities.formatDate(new Date(file_name.getDateCreated()), "GMT", "yyyy_MM_dd");
      
      file_name.setName(filename_format+"_"+file_name);
}

Best Answer

There are many approaches to this resolving this question. The key is how to establish whether the first eleven characters of the file name signify a "renamed" file.

The rename format is "yyyy_MM_dd_". The date is separated from the file name by an "underscore". Had you used a different, perhaps unique, separator then you would need only test for that character.

The following script uses the indexOf method (which is zero-based) to test for each of the three underscore characters.

If the combined result is true, then it is a renamed file and the date prefix is stripped from the name to provide the "original" file name for renaming. If the result is false, then the file has not been renamed, and you can continue as normal.


function wa_146451() {
  
  // these options provided for proof-of-concept testing
  var filename = "filename.doc"
  var filenameRenamed = "2020_09_27_filename.doc";
  var filenameSearch = filenameRenamed;
  
  
  // if a file has been renamed, then it will contain 3 underscores
  // at character 4, 7 and 10 (zero-based)

  // create variable for underscore
  var searchTerm = "_";
  
  // search for each of the first three underscore characters
  var indexOfFirst = filenameSearch.indexOf(searchTerm);
  var indexOfSecond = filenameSearch.indexOf(searchTerm,(indexOfFirst + 1));
  var indexOfThird = filenameSearch.indexOf(searchTerm,(indexOfSecond + 1));
  // Logger.log("DEBUG: IndexOf1 = "+indexOfFirst+", IndexOf2 = "+indexOfSecond+", IndexOf3 = "+indexOfThird);
  
  // test for underscores at position 4, 7 and 10 (each must be true)
  if (indexOfFirst === 4 && indexOfSecond === 7 && indexOfThird === 10){
    //Logger.log("DEBUG: File name = "+filename+"\nthis is a renamed file");
    
    // reconstruct the original file name
    var filename = filenameSearch.substr(11);
    // Logger.log("DEBUG: the orginal file name = "+originalFileName);
  }
  else
  {
    // this is a unrenamed file
    // Logger.log("DEBUG: File name = "+filename+"\nthis is NOT a renamed file")
  }
  
  // now rename the file
  Logger.log("DEBUG: the file is "+filename);
}