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 download (please use 'File' > 'Make a copy' for your own version).

33 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