Saturday 7 September 2019

Bulk rename files in Google Drive

As of February 2022 this tool is now available as a Google Workspace Marketplace Add-on.

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.

Download
Bulk rename files in Google Drive download (please use 'File' > 'Make a copy' for your own version).
As of February 2022 this is now available as a Google Workspace Marketplace Add-on.

43 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
  5. Is it possible to get this to work in Shared drives. All I get is an error.

    ReplyDelete
    Replies
    1. Hi

      This does work in Shared drives.

      Kind regards
      Phil

      Delete
    2. Hello. This is an amazing script, thank you so much! I have it working in my own drive on a test folder. When I move it into a shared drive and create a shared test folder, I get the error "unable to get Google Drive folder please check correct ID". Could there be some issue with permissions or access to shared drives that present a different fileID and doesn't allow access?

      Delete
    3. Hi

      Thank you for the feedback.

      I have tested this in Shared drives and it works fine. Are you happy you are providing the correct Folder ID from the Shared drive?

      Kind regards
      Phi

      Delete
    4. Dear Phil,

      I have figured out how to make it work but I don't understand why, and I'd like to be able to explain it to others when I share this automation.

      In my own drive (not the shared drive), the folder ID will be something like:
      1WUTq_CddzKj-gCh7nolPGhYWknKydZFO - and the script works

      In the shared drive, the folder ID of the folders that I create (I am not an administrator of the shared drive) will be something like:
      1r3AUiQEXhd5qWtnDPZgy_P6mZS6FY9_f?ths=true

      The script is unable to find this fileID. But it worked when I removed the last part of the file ID from the ? onward, using this as the file ID instead:
      1r3AUiQEXhd5qWtnDPZgy_P6mZS6FY9_f

      Do you know what the string ?ths=true means and why it works when I remove it?

      So many thanks to you!
      Linda

      Delete
    5. Hi Linda

      Many thanks for sharing your troubleshooting. I'm afraid I've never come across that part of the Url - the ID has always been just the long string of letters/numbers for me.

      I tried to Google it too or see if various Shared drive permissions were the cause but nothing showed up.

      I am glad you have been able to get sorted now though!

      Kind regards
      Phil

      Delete
  6. How would I get started making use of this? Where does the script code actually go?

    ReplyDelete
    Replies
    1. Hi

      There is a link to the Google Sheet just above all these comments (or a direct link here: http://bit.ly/2lHw5Bq) - you will need to 'Make a copy' so you have your own version.
      The code is all there and ready for people to use straight out of the box. You only need to complete the 3 pieces of requested information.

      Kind regards
      Phil

      Delete
  7. hi,
    awesome script! Do you think it is possible to make it work also for nested folders or sheets, that are one level deeper. Meaning I have a folder, that is named "Copy - Hello"
    and in this folder I have 3 sheets named "Copy - Bye" and I want to get rid of the "copy -" part.

    Best
    Nico

    ReplyDelete
    Replies
    1. Hi Nico

      Thank you!

      Yes that would be possible to do - you would need to iterate through the sub-folders/files. Admittedly it's not necessarily a quick thing to add to the code. This blog post for changing ownership of items gives you an example of iterating through: https://www.pbainbridge.co.uk/2020/04/bulk-change-ownership-of-google-drive.html

      Kind regards
      Phil

      Delete
  8. Hi, I have this code to rename all subfolders within a parent folder. But it seems it's not working, I will appreciate if someone can help. Thanks!


    function myFunction()
    { // **// this worked for me rename files of type CSV to processed**

    var sh = SpreadsheetApp.getActiveSheet();
    //var foldername = 'Test Folder';
    var folders = DriveApp.getFoldersByName('Test Folder'); // change accordingly to folder ID
    var folders1 = folders.next();


    //var files = foldersnext.getFolders();
    var sub1 = folders.getFoldersByName("Testing");
    // new name of csv that I want to rename the file within my google drive folder
    var newName = "aaaaa";
    while(sub1.hasNext())
    {
    // iterate throught the csv files available
    var file = sub1.next()
    // will rename all csv's to processed
    file.setName(newName);
    }
    }

    ReplyDelete
    Replies
    1. Hi

      What errors are you getting? There are lots of duplicate bits and commented out code which is making it a bit awkward to read. Have you tried taking my code and just switching out for 'getFolders' (https://developers.google.com/apps-script/reference/drive/drive-app#getfolders) instead?

      Kind regards
      Phil

      Delete
  9. This is great. How can you use this to add something to the front of the file name? I want to retain the original title but add the work ARCHIVED in front of it so I will know when doing a Drive search immediately that this file is outdated and only archived for historical reasons.

    ReplyDelete
    Replies
    1. Hi

      Yes you would tweak line 70 with something like this:

      var newName = subFileName.replace(searchString, "ARCHIVED" + replaceString)

      Kind regards
      Phil

      Delete
  10. Doesn't work for me ... mostly it's randomly deleting some words in the replacement string. And adding characters.

    ReplyDelete
    Replies
    1. Hi Abhimanyu

      Are you able to provide any further information on what you are entering into the Sheet please?

      Kind regards
      Phil

      Delete
  11. Works perfectly! This is exactly what I need. Thank you for sharing

    ReplyDelete
    Replies
    1. Excellent! Thank you very much for the feedback.

      Kind regards
      Phil

      Delete
  12. First off - Thanks. Atleast it assuages my concern of privacy -- the script is hosted and running in my own drive with my own credentials. So, I guess it's safe?

    Secondly -- It threw up an error on first run, for me. Although the error dialogue provided no description. But when I just re-ran it again, on a hunch, it displayed a dialogue requesting access permission. Once allowed, it ran normally for the mandatory 'Copy of' stripping requirement that Google's all-knowing-but-mindless stupidity imposes on all of us.

    I'm going to copy this Google Sheet to my other Google accounts and try on all of them. Will report back the results for the rest of the visitors of this wonderful blog.

    ReplyDelete
    Replies
    1. Hi N-Man

      Yes taking a copy means the file is yours in your Google Drive. All of my code is open and available to scrutiny too.

      Thank you for your nice feedback.

      Kind regards
      Phil

      Delete
  13. Hello, thank you for creating such a useful tool. I'm encountering something here: nothing is replaced the first time I run the tool, however the second time I run it with a different string/content in the Replace field, it takes the string/content from my first run instead. Do you know what is going on? Thanks.

    ReplyDelete
    Replies
    1. Hi Van

      Thank you.

      The very first time you try to run the tool and accept the authorisation popup, it will not run immediately - so you will need to click it again. From this point onwards however it will work first time.

      My only other thought is whether there is a slight lag in Google Drive after you have performed the Replace. I would suggest creating some demo files in a folder, running the tool once on them, refreshing the Drive page and checking the changes have been applied.

      There's nothing in the tool that creates a delay as you mention.

      Kind regards
      Phil

      Delete
    2. Hi Phil,

      Thank you for the response. It is indeed due to lag issue. Another thing I noticed was that after I changed the Replace box content, I was supposed to click on a random cell to make Excel commit and saves that change. If I click "Start" when I'm still on that Replace cell, it won't work, as in it will take the content that was previously saved.

      I was able to rename lots of files with the tool, so thanks again!

      Delete
    3. Hi Van

      Great!

      Yes it's interesting you mention that about needing to click out of the cell - I'd notice a couple of other people get caught out with that (I think I click away by default). Other than tweaking the guidance information in the Sheet I'm not aware of another way around that one as it's more Google Sheets at fault.

      Kind regards
      Phil

      Delete
  14. Hi Phil,
    Thanks for this script it helped indeed. Could you help me if I want to rename all the folders with an incremental suffix ? for example i have folders for session wise content named as session 25, session 26, session 27...I want to change them with Session 1, Session 2, Session 3 (so basically I want to re-number them.

    ReplyDelete
    Replies
    1. Hi

      Thank you.

      That is something that would be possible, it's not a 'tweak' per se to this tool but a variation of it. You would need to include the file name within the loop as you go through each file (so that you can create the incrementation you also want).

      Kind regards
      Phil

      Delete
  15. Thanks for the script Phil. Loving it. Have around 5 folders with more than 50 files in each. Managed to rename them easily with your Script. Thank you so much again.

    ReplyDelete
    Replies
    1. Hi Mikhail

      Thank you for your nice comments. Glad to hear it has been useful to you.

      Kind regards
      Phil

      Delete
  16. How would you add multiple tags (I think Google calls them 'Labels') using data from a specific Sheets file? Is the tag/labelling feature still supported in Drive? I try to find it manually, but see no such features, despite 2022 support documentation saying it's possible?

    Just so it's not bogged down, I'm really just wondering how to add labels/tags while renaming the file... pulling the values from somewhere else is a separate topic I suppose.

    ReplyDelete
    Replies
    1. Hi

      Thank you for your comment.

      If this is the new Google Drive labels for files feature (https://support.google.com/a/users/answer/10898474) then I'm afraid I don't have access to it on any of my accounts to try it out. Sorry.

      Kind regards
      Phil

      Delete