Skip to content

Instantly share code, notes, and snippets.

@pa-0
Forked from edoan/Drive rename.js
Last active March 27, 2024 13:58
Show Gist options
  • Save pa-0/f005ef16fcf32fbc2c55ca1d8333f810 to your computer and use it in GitHub Desktop.
Save pa-0/f005ef16fcf32fbc2c55ca1d8333f810 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
## These are the comments from the original that I forked (alternative scripts availabl)
//a script that will work with subfolders.
//This should work for every file in your drive.
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);
};
};
}
//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);
};
};
}
//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 of"
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);
}
/* 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);
};
};
}

Notes

I've copied and pasted the comments from the original gist that I forked, as those include some alternative methods :)

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