-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoogle-sharedrivecopy.gs
82 lines (59 loc) · 3.87 KB
/
google-sharedrivecopy.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function start() {
//global variables to facilitate logging and email
PropertiesService.getScriptProperties().setProperty('sourceFolderIdGlobal', 'xxx');
PropertiesService.getScriptProperties().setProperty('targetParentFolderIdGlobal', 'xxx');
PropertiesService.getScriptProperties().setProperty('StartTime', new Date()); //get timestamp to record the intial execution time
{
// Google App Script to the rescue
// Code adapted from this artcile https://www.labnol.org/code/19979-copy-folders-drive
// Thank you Amit Agarwal https://github.com/labnol
// This script will copy all files and folders from the source Folder ID to a Destination Folder ID
// The Source Folder name will be created in the destination folder
// If you have been driven insane by Google Drives inability to copy a folder structure from MyDrive to a Shared drive BOOM!
//
// Important note: Enable the Drive service in the script editor https://developers.google.com/apps-script/advanced/drive
var sourceFolderId = PropertiesService.getScriptProperties().getProperty('sourceFolderIdGlobal'); //Folder ID of the folder structure you want to copy
//Add target folder ID if you want the dest to be somewhere other than the root of Mydrive
var targetParentFolderId = PropertiesService.getScriptProperties().getProperty('targetParentFolderIdGlobal');
var sourceFolder = DriveApp.getFolderById(sourceFolderId); //Source Folder
if (targetParentFolderId.length > 0 ) {
// If the target folder ID has been provided, create the target folder within that folder
var targetParent = DriveApp.getFolderById(targetParentFolderId); //Parent Folder (target)
Logger.log( "Create target folder: " + sourceFolder.getName() + " in " + targetParent.getName()); //copy console entry into stored logger
var target = targetParent.createFolder(sourceFolder.getName()); //Create the source folder we are copying
} else {
// If the target folder ID is empty, create the target folder within the root of Mydrive
Logger.log( "Create target folder in Mydrive: " + sourceFolder.getName()); //copy console entry into stored logger
Logger.log( "Create target folder in Mydrive: " + sourceFolder.getName()); //copy console entry into stored logger
var target = DriveApp.createFolder(sourceFolder.getName()); //Create the source folder
}
//Here we go
Logger.clear; //Clear stored log data before next execution so we don't keep sending old data
var recipient = 'email@email.com'; //Destination email address to notify that script has started (for triggered executions)
var subject = 'MyDrive Copy Log';
var body = 'MyDrive Copy Started for folder pair: '+sourceFolderId+' to '+targetParentFolderId+"\n This script execution was started at "+ PropertiesService.getScriptProperties().getProperty('StartTime');
MailApp.sendEmail(recipient, subject, body);
copyFolder(sourceFolder, target);
Logger.log("Copy finish"); //copy console entry into stored logger
var recipient = 'email@email.com'; //Destination email address to send final completion notice and the logged console data
var subject = 'MyDrive Copy Log';
var body = 'MyDrive Copy finished at '+ new Date()+';'+" The execution log is as follows: \n" +Logger.getLog();
MailApp.sendEmail(recipient, subject, body);
}
}
function copyFolder(source, target) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
Logger.log( "Copying file: " + file.getName()); //copy console entry into stored logger
file.makeCopy(file.getName(), target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
Logger.log( "Copying folder: " + folderName); //copy console entry into stored logger
var targetFolder = target.createFolder(folderName);
copyFolder(subFolder, targetFolder);
}
}