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 NameNew File Name
boring.doccranberrySauce.doc
sad.doc cranberrySauce1.doc
yawn.doc cranberrySauce2.doc

The Code

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 out side 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 chose 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:

FileMIMEExtension
MS Wordapplication/msword.doc
MS Excelapplication/vnd.ms-excel.xls
Adobe Portable
Document Format
application/pdf.pdf
MS PowerPointapplication/vnd.ms-powerpoint.ppt
JPEG Imageimage/jpeg.jpeg, .jpg
Portable Network
Graphics (PNG)
image/png.png
MPEG-4 Audioaudio/mp4.mp4a

There are heaps more file types and there is 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.

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. 

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!!!

~Yagi

Leave a Reply