Basically, I have a bunch of files in a shared google drive, let's say:


I am trying to get rid of all of the "123"'s and replace them with an empty space, or just replace "123_" with "_". I am not particularly familiar with google scripts (I do understand basic programming logic though), but yeah, basically I was wondering if anyone knows how I could make a script for this as there is a very large amount of files. Thanks ever so much.

  • 2
    Please add a brief description of your search/research efforts as is suggested by How to Ask
    – Rubén
    Aug 23, 2020 at 15:48
  • 1
    Sometimes it is hard to know just where to start. Google "google drive script bulk rename files" you will get a LOT of hits. Many will reference the Bulk Renamer for Drive - about which I know nothing and have no opinion. You'll also find some general scripts such as Bulk rename files in Google Drive which I tested and it does exactly what it says on the tin. if you need help with the latter, let me know.
    – Tedinoz
    Aug 24, 2020 at 12:22

1 Answer 1


In a given folder of Google Drive, you have a number of jpg images, each with a consistent file name structure ("AA123_y.jpg, where y is a single or multi-digit sequence number).

You want to rename these files to remove the third, fourth and fifth digits ("123").

For example, "AA123_1.jpg" -> "AA_1.jpg"; "AA123_1003.jpg" -> "AA_1003.jpg".

function renamejpgs() {

  // A given Goiogle Drive folder contains jpg files.
  // The files have a consistent naming structure:
  // "AA123_y.jpg, where y is a single or multi-digit sequence number.
  // The function renames the files by removing the third, fourth, fifth and sixth characters of the file name and substituting a single"underscore"
  // For example, "AA123_1.jpg" -> "AA_1.jpg"
  // set the mime type/file type to be renamed
  var mimetype = 'image/jpeg';
  // get the folder ID; note the id is a string
  var folderid = "<<insert your folder ID>>"
  // getFoldersById = Gets a specific folders in the user's Drive
  var folder = DriveApp.getFolderById(folderid)
  // get  files in this folder
  // myfiles is a File Iterator
  var myfiles = folder.getFiles();

  // loop through files in this folder
  while (myfiles.hasNext()) { 
    var myfile = myfiles.next();   
    var myname = myfile.getName();
    var ftype = myfile.getMimeType();
    // find the next file that matches the mime type
    var indexOfFirst = ftype.indexOf(mimetype);

    if (indexOfFirst != -1){ 
      // the next file was an image
      // edit the file name
      var fname = myname.replace("123_", "_");
      // update the file name
    } //end if
  } // end while loop through main folder
  return false;


  • getFolderById() is used to identify the specific folder on Google Drive
  • var mimetype = 'image/jpeg'; - this is a user defined variable; in this case the mime type is used to limit the files whose names will be changed to only jpg files.
  • getMimeType(); returns the mime type of a specific file.
  • ftype.indexOf(mimetype ); - a test to see if the file is the given mime type. indexOf returns "-1" if the index is not found, hence the IF statement.
  • var fname = myname.replace("123_", "_"); - uses the Javascript "replace" method to remove the third, fourth and fifth character and sixth characters which is ="123_". The script replaces the underscore because there is possibility of a file named "AA123_123.jpg", in which case the second "123" is not to be replaced.
  • myfile.setName(fname); - updates the file name

Sample - Before


Sample - After


Note: the jpg files have been renamed, but the spreadsheet (which has a similar naming pattern) has not been renamed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.