Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove "Copy of" filename prefix when copying an entire folder from one Google Drive account to another Google Drive account
/* Need to copy an entire folder from one Google Drive account to another Google Drive account?
* 1. Right-click on original folder in Google Drive
* 2. Share with the destination Google account
* 3. Go into destination account's Google Drive
* 4. Find the shared folder under "Shared with me"
* 5. Select all the files (Ctrl-A / Cmd-A)
* 6. Right-click, Make a copy
* 7. Create a New Folder in destination account's Google Drive
* 8. Go to Recent
* 9. Select all recently copied files and move to the new folder
* 10. Great, except all the filenames begin with "Copy of"xxxxxx.txt
* 11. Create a new Apps Script, perhaps using Google Sheets, and use the below script to remove the "Copy of" filename prefix!
*/
function fileRename() {
var folders = DriveApp.getFoldersByName('FOLDERNAME');
var folder = folders.next();
var files = folder.getFiles();
while(files.hasNext()){
var file = files.next()
var fileName = file.getName();
if (fileName.indexOf('Copy of ') > -1) {
fileName= fileName.split('Copy of ')[1];
file.setName(fileName);
};
};
}
@maxjdugan
Copy link

maxjdugan commented Aug 2, 2021

Works like a dream. Thank you!

@jbriano-github
Copy link

jbriano-github commented Aug 4, 2021

Works great! Thank you. I'd love to get a script that will work with subfolders.

@millansoft
Copy link

millansoft commented Jan 7, 2022

Hello, from the google apps script, I got the following error: "Exception: Cannot retrieve the next object: iterator has reached the end."

@jademanta
Copy link

jademanta commented Jan 14, 2022

Worked great for me. Thanks!

@fachrisama
Copy link

fachrisama commented Feb 25, 2022

Thank you!

@FSJ2812
Copy link

FSJ2812 commented Jun 2, 2022

Works like a charm!!

@FSJ2812
Copy link

FSJ2812 commented Jun 2, 2022

Hello, from the google apps script, I got the following error: "Exception: Cannot retrieve the next object: iterator has reached the end."

Then there's no folder in such name.

@FSJ2812
Copy link

FSJ2812 commented Jun 2, 2022

Works great! Thank you. I'd love to get a script that will work with subfolders.

function fileRename() {
var files = DriveApp.getFiles();

while(files.hasNext()){
var file = files.next()
var fileName = file.getName();
if (fileName.indexOf('Copy of ') > -1) {
fileName= fileName.split('Copy of ')[1];
file.setName(fileName);
};
};
}

This works for every file in your drive.
If theres any flaw, someone can point it out..!

@ccfarah
Copy link

ccfarah commented Jul 4, 2022

Thanks @edoan ! Works fine. I might just change step 11, and create a new Apps Script directly within Google Drive, which is slightly more economical than creating a Google Sheet just to create an Apps Script. Very minor amendment for the wonderful contribution!

@CashBlood
Copy link

CashBlood commented Sep 22, 2022

Works great! Thank you. I'd love to get a script that will work with subfolders.

function fileRename() { var files = DriveApp.getFiles();

while(files.hasNext()){ var file = files.next() var fileName = file.getName(); if (fileName.indexOf('Copy of ') > -1) { fileName= fileName.split('Copy of ')[1]; file.setName(fileName); }; }; }

This works for every file in your drive. If there's any flaw, someone can point it out..!

This really helped me a lot, thank you! I have another question, and I am a novice for this kind of stuff, but how could I remove ".jpg" from the end of my photo titles in drive?

@mezzaveli
Copy link

mezzaveli commented Oct 25, 2022

Works a treat! Thanks!!

@Bhaskar0227
Copy link

Bhaskar0227 commented Nov 11, 2022

Can't thank you enough!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment