Google Apps Script – How to Change the File Name of Non-Native Files like MS Word Docs, PDF’s and Excel files.

File Name Change - Google Apps Script

Google Apps Script, DriveApp

I had an unexpected need to change some non-native file names – in my case, MS Word docs – to something else to appease my masters. Being the resident Igor, I delighted in the task. 

The Problem

I had a folder of MS Word documents on my Google Drive that needed to all be renamed. They all needed the same name with an index number to distinguish that they were different files. 

There were however other file types in the folder. 

The problem with non-native files in Google Drive is that it’s a little tricky to find their ID. Besides, I just wanted to change ALL files that were MS Word in the folder. 

What I would need to do is search for all files inside my selected folder that are of MS Word type and rename them with the same name but with a counter at the end. For example:

Old File Name New File Name
boring.doc cranberrySauce.doc
sad.doc cranberrySauce1.doc
yawn.doc cranberrySauce2.doc

The Code

Hire me for our next Google Workspace project.

start()

Start (Lines 23-32) is just a function I have used to add my variables and run my main function non_native_file_name_changer. You can put these variables in any function you want or put them outside of a function if you are that way inclined. 

non_native_file_name_changer()

The non_native_file_name_changer function takes 4 arguments:

  1.  Folder ID
  2. File Name: the new name you want to give the file
  3. File Type: the MIME file type
  4. Iterator: Do you want each file to have a new number at the end?

On line 6 the folder variable gets the folder id that we are searching in using the DriveApp class.  

On the next line, we then look in that folder location for all the files by the type – in our case, Ms Word. Looking back down at line 27, you can see that what we have chosen is "application/msword".

File Types MIME

MIME stands for Multipurpose Internet Mail Extensions and is just a standardised way of identifying file formats. 

You will most likely come across the following formats when working in Google Drive:

File MIME Extension
MS Word application/msword .doc
MS Excel application/vnd.ms-excel .xls
Adobe Portable
Document Format
application/pdf .pdf
MS PowerPoint application/vnd.ms-powerpoint .ppt
JPEG Image image/jpeg .jpeg, .jpg
Portable Network
Graphics (PNG)
image/png .png
MPEG-4 Audio audio/mp4 .mp4a

There are heaps more file types and there are also MIME types for Google files.

…back to the code…

The Iterator

Photo by, Barry Stock

No, that is not a re-branding of the Count from Sesame Street.

Line 8 is the count we will use if an iterator is requested by the user. 

You can see on line 13 we have an if statement that asks if the user has requested an iterator. The iterator is the 4th argument in the function and if set to true then a number will appear after each file rename. 

You can see in the example on line 28 that we have set the iterator to true

Looping Through File Results

Google Apps Script will create a generator (Kind of like a list) of all the files with the file extension type you requested. 

We then use a while loop in conjunction with the hasNext() built-in method to loop through all the files and do something with them.

🐐You can support me for free by using this Amazon affiliate link in your next tech purchase :Computers & Stuff! 🐐

Inside the loop on line 11 you will see the next() method that identifies the next item in the generator. We set this as a variable file that we use to then setName to either our fileName with count or just fileName

Example

Example 1 – New File Name with Iterator

Let’s create some new file names with our sample word documents. We will want to have a new number at the end of the file name. 

Create and Publish Google Workspace Add-ons with Apps Script Course 300px
File Name Change - Google Apps Script

Example 2 – New File Name Only

How about we just rename some PDF files without the iterator.  Let’s change the name to “Gravy”.

filename Change No Iterator - Google Apps Script

That’s it! There are a lot of things you can do with this code just with a few tweaks. One challenge you might want to consider is to see if you can do file name changes based on column data From a Google Sheet. 

Good luck, mates!!!

Create and Publish a Google Workspace Add-on with Apps Script Course

Need help with Google Workspace development?

My team of experts can help you with all of your needs, from custom app development to integrations and security. We have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.

Schedule a free consultation today to discuss your needs and get started or learn more about our services here.


~Yagi

2 thoughts on “Google Apps Script – How to Change the File Name of Non-Native Files like MS Word Docs, PDF’s and Excel files.”

  1. Hi, thanks for sharing the script.
    I can’t get it to run as it returns an error:

    SyntaxError: Invalid or unexpected token (line 26, file “Code.gs”)

    1. Hi Mike,

      What does your line 26 look like? Id your folder ID wrapped in “”?

      Cheers,

      Yagi

Leave a Reply