As of February 2022 this tool is now available as a Google Workspace Marketplace Add-on.
Download
As of February 2022 this is now available as a Google Workspace Marketplace Add-on.
I'm Phil Bainbridge and I aim to share my experiences of IT projects I've worked on as I continue to learn and develop, through this blog. My main coding language is Google Apps Script (with JavaScript), but there is also some: PowerShell, HTML, SQL and jQuery.
thank you for the script. it's really helpful :D
ReplyDeleteThank you for the feedback. Glad it was useful.
DeleteIs it possible to add functions for prepend to beginning of filename, or append to end of file name?
ReplyDeleteJesse is depends what you want to do: you could add some static text to append to each of the filenames, for instance:
Deletevar newName = subFileName.replace(searchString, replaceString + "EXTRA TEXT HERE");
Ta
Phil
Awesome. And for append at beginning , could that be switched to:
Deletevar newName = subFileName.replace(searchString, "EXTRA TEXT HERE" + replaceString );
?
Yep that would work.
DeleteHi 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?
ReplyDeleteHi
DeleteYes 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
Ok, great. Thanks for your help, Phil! I'll look into the other post.
DeleteI 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
ReplyDeleteI changed subFile.setName(newName) > subFile.setDescription(newName);
DeleteThat's great!
DeleteKind regards
Phil
Is it possible to get this to work in Shared drives. All I get is an error.
ReplyDeleteHi
DeleteThis does work in Shared drives.
Kind regards
Phil
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?
DeleteHi
DeleteThank 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
Dear Phil,
DeleteI 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
Hi Linda
DeleteMany 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
How would I get started making use of this? Where does the script code actually go?
ReplyDeleteHi
DeleteThere 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
Nice! That worked well!
ReplyDeletehi,
ReplyDeleteawesome 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
Hi Nico
DeleteThank 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
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!
ReplyDeletefunction 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);
}
}
Hi
DeleteWhat 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
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.
ReplyDeleteHi
DeleteYes you would tweak line 70 with something like this:
var newName = subFileName.replace(searchString, "ARCHIVED" + replaceString)
Kind regards
Phil
Doesn't work for me ... mostly it's randomly deleting some words in the replacement string. And adding characters.
ReplyDeleteHi Abhimanyu
DeleteAre you able to provide any further information on what you are entering into the Sheet please?
Kind regards
Phil
Works perfectly! This is exactly what I need. Thank you for sharing
ReplyDeleteExcellent! Thank you very much for the feedback.
DeleteKind regards
Phil
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?
ReplyDeleteSecondly -- 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.
Hi N-Man
DeleteYes 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
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.
ReplyDeleteHi Van
DeleteThank 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
Hi Phil,
DeleteThank 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!
Hi Van
DeleteGreat!
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
Hi Phil,
ReplyDeleteThanks 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.
Hi
DeleteThank 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
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.
ReplyDeleteHi Mikhail
DeleteThank you for your nice comments. Glad to hear it has been useful to you.
Kind regards
Phil
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?
ReplyDeleteJust 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.
Hi
DeleteThank 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