Created
February 20, 2020 04:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
}; | |
}; | |
} |
nice, i didnt know that these scripts exists, now i write a version that copy from the a shared folder, by uid:
function myFunction() {
const ORIGIN_FOLDER = '<folder id>'
const DESTIN_FOLDER = '<folder id>'
const SKIP_FOLDERS_ALREADY_IN_DESTIN = true;
const USE_MAKE_COPY = false;
let fo = DriveApp.getFolderById(ORIGIN_FOLDER);
let fd = DriveApp.getFolderById(DESTIN_FOLDER);
let rec = (fo,fd)=>{
let files = fo.getFiles();
while (files.hasNext()) {
let sub_fo = files.next();
const sub_fo_name = sub_fo.getName();
if (!fd.getFilesByName(sub_fo_name).hasNext()) {
console.log('coping: '+sub_fo_name);
if (USE_MAKE_COPY) {
let newf = sub_fo.makeCopy().moveTo(fd);
newf.setName(sub_fo_name);
} else {
fd.createFile(sub_fo.getBlob()).setName(sub_fo_name);
}
} else {
console.log('file already exists: '+sub_fo_name);
}
}
let folders = fo.getFolders();
while (folders.hasNext()) {
let sub_fo = folders.next();
const sub_fo_name = sub_fo.getName();
let sub_fd;
let fd_it = fd.getFoldersByName(sub_fo_name);
if (!fd_it.hasNext()) {
console.log('make dir: '+sub_fo_name)
sub_fd = fd.createFolder(sub_fo_name);
if (SKIP_FOLDERS_ALREADY_IN_DESTIN) {
rec(sub_fo, sub_fd);
}
} else {
sub_fd = fd_it.next();
}
if (!SKIP_FOLDERS_ALREADY_IN_DESTIN) {
rec(sub_fo, sub_fd);
}
}
}
rec(fo, fd);
}
to get folder id open the folder you want and look the url: "https://drive .... folders/<folder-id>"
unfortunaly, makeCopy not work, so i used getBlob, poorly eficient but no hand work needed...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, made an account just to comment!