Pages

Saturday, 7 September 2019

Bulk rename files in Google Drive

The following Google Apps Script is designed to automate bulk renaming of multiple files within a single Google Drive folder. It has been developed as a Google Sheet with flexibility towards entering your own search pattern and replacement string as you see fit.
Screenshot of File renamer text fields
Screenshot of File renamer text fields
There is some validation within the script to help check for errors before proceeding - try/catch when getting the Google Drive folder and 'getui' to display an 'alert' popup if there is a problem with the entered ID.

To perform the actual file rename we use 'replace' once we have done a 'getName'. After which we have a new variable called newName which can then be used to 'setName' back on the file itself.

Bulk rename files in Google Drive.xlsx

12 comments:

  1. thank you for the script. it's really helpful :D

    ReplyDelete
    Replies
    1. Thank you for the feedback. Glad it was useful.

      Delete
  2. Is it possible to add functions for prepend to beginning of filename, or append to end of file name?

    ReplyDelete
    Replies
    1. Jesse is depends what you want to do: you could add some static text to append to each of the filenames, for instance:

      var newName = subFileName.replace(searchString, replaceString + "EXTRA TEXT HERE");

      Ta
      Phil

      Delete
    2. Awesome. And for append at beginning , could that be switched to:

      var newName = subFileName.replace(searchString, "EXTRA TEXT HERE" + replaceString );

      ?

      Delete
  3. Hi Phil, Been searching online for days before I stumbled onto your post. Is it possible to rename using data from the spreadsheet? For instance, I have a Google sheet with existing file names for images in a folder (Column C), along with First and Last Names (Columns F & G). I would like to rename a file based on the First & Last names for that row "LastNameFirstName." Is that possible with some tweaks to the script?

    ReplyDelete
    Replies
    1. Hi

      Yes that is feasible but admittedly it may be more than a 'tweak' to this script as you would need to introduce a Loop to iterate through the spreadsheet rows.

      I think the best bet in the first instance is to take a look at this Blog post which creates Google Drive folders and uses data from the spreadsheet for their name: https://www.pbainbridge.co.uk/2019/10/create-alphabetised-folders-in-google.html

      Just thinking off the top of my head ... you could loop through the 'var subFiles' array (Line 57) and in this loop have it get the first row of data to use as the filename. Then when the loop repeats it will start on the next file and jump down a row in the spreadsheet.

      Ta
      Phil

      Delete
    2. Ok, great. Thanks for your help, Phil! I'll look into the other post.

      Delete
  4. I would like to find and replace my file description instead of the file name, but I can not find the solution. How can I do that? Thanks

    ReplyDelete
    Replies
    1. I changed subFile.setName(newName) > subFile.setDescription(newName);

      Delete
    2. That's great!

      Kind regards
      Phil

      Delete