Skip to content

Instantly share code, notes, and snippets.

@edoan
Created February 20, 2020 04:03
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save edoan/181094b112b848404dd814d44b71369c to your computer and use it in GitHub Desktop.
Save edoan/181094b112b848404dd814d44b71369c to your computer and use it in GitHub Desktop.
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

Works like a dream. Thank you!

@jbriano-github
Copy link

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

@millansoft
Copy link

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

Worked great for me. Thanks!

@fachrisama
Copy link

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

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

Works a treat! Thanks!!

@Bhaskar0227
Copy link

Can't thank you enough!

@NiteBone
Copy link

Thank you. Both all and specific folders script worked for me. Saved me so much time and trouble. This was an awesome find.

@alamonelf
Copy link

TY

@SenaKaito
Copy link

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

Thanks man.
It works (〃 ̄︶ ̄)人( ̄︶ ̄〃)

@kobozo-ship-it
Copy link

Fantastic, thank you!

@data-kalle
Copy link

Just wanted to leave a huge thank you (!!) for this script! It works flawlessly. I made a Google Apps script inside the drive folder with the files, put in the folder name in this function, adjusted the prefix and it was all solved without any hassle. You cannot imagine how much help this has been.

@baigern
Copy link

baigern commented Aug 9, 2023

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

I keep getting the following error

"Exception: Access denied: DriveApp.
fileRename @ Code.gs:9"

@Skwalin
Copy link

Skwalin commented Aug 9, 2023

I reworked this a bit to be fully recursive and to include the parent folder ID.

function fileRename() {
  var parentfolder = DriveApp.getFolderById('XXXXXXXXXXXXXXXXXXXXXXX');
  var recursive = true;

  getFilesFromFolder(parentfolder);

  var folders = parentfolder.getFolders();
  while(folders.hasNext() && recursive){
    var folder = folders.next();
    getFilesFromFolder(folder);
  }
}

function getFilesFromFolder(folder){
  var folderName = folder.getName();
  Logger.log(folderName);
  var files = folder.getFiles();
  while(files.hasNext()){
    var file = files.next()
    var fileName = file.getName();
    if (fileName.indexOf('Copy of ') > -1) {
        Logger.log(fileName);
        fileName= fileName.split('Copy of ')[1];
        file.setName(fileName);
    };
  };
}

@Vegz78
Copy link

Vegz78 commented Sep 11, 2023

Thanks, @edoan! -Saved my day!

@heavenchou
Copy link

I'm using the Chinese version, and my original file is named "a.pdf." After making a copy, the file is named "a.pdf 的副本" (a copy of a.pdf). I can quickly change it back to "a.pdf," but when I download it, the default download filename becomes "a.pdf.pdf 的副本.pdf 的副本" (repeated twice). Does the English version have the same issue? I can't find a way to fix the default download filename.

@Skwalin
Copy link

Skwalin commented Oct 12, 2023

I'm using the Chinese version, and my original file is named "a.pdf." After making a copy, the file is named "a.pdf 的副本" (a copy of a.pdf). I can quickly change it back to "a.pdf," but when I download it, the default download filename becomes "a.pdf.pdf 的副本.pdf 的副本" (repeated twice). Does the English version have the same issue? I can't find a way to fix the default download filename.

In my comment, the section that rewrites it is this, which should be able to be altered for other languages:

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

@heavenchou
Copy link

I can change the name, but when I download it, the default file name is very "ugly". I wonder if I can change the default file name. :(

image

@heavenchou
Copy link

I asked Google One and they said this problem does not occur in the English version. As for whether it can be solved, I have to report it to google.drive. Thank you all.

@mcosstick
Copy link

mcosstick commented Nov 17, 2023

That is an excellent script. Below is some updated code you can add to a gsheet script. It adds a menu item to execute the script to the gsheet menu bar and provides a popup box so you can enter the folder name that contains the files with "Copy to " that need to be fixed up.

function onOpen() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
ui.createMenu('Remove "Copy of" from start of file names in a folder')
.addItem('Start script', 'fileRename')
.addToUi();
};

function fileRename() {
var inputFolder = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
if (inputFolder === "") {
Browser.msgBox('Remove "Copy of" from the start of file names in this folder:');
return;
}

var folders = DriveApp.getFoldersByName(inputFolder);
var folder = folders.next();
var files = folder.getFiles();
var fileCnt=0;
var renamedCnt = 0;

while(files.hasNext()){
fileCnt++;
var file = files.next()
var fileName = file.getName();
if (fileName.indexOf('Copy of ') > -1) {
fileName= fileName.split('Copy of ')[1];
file.setName(fileName);
renamedCnt++;
};
};
SpreadsheetApp.getUi().alert('Removed "Copy of" from: '+renamedCnt+" Total Files Processed:"+fileCnt);
}

@CharlotteDiamond
Copy link

Thank you, it was really useful!

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