複製 Google 雲端的資料夾

前言

最近在 PTT 上看到有好心的大大分享了 Python 的教學資源。因為資料很多,不想全部下載下來,我就開始尋找有沒有直接把資料複製到自己雲端的方法

除了一個一個按右鍵建立副本外,我找到一個以 Google 試算表為介面,執行 Google App Script 後,就能把該 Folder ID 的資料夾複製到自己的雲端。

不過我實際使用後,資料夾內的檔案太多的話,會容易失敗的樣子

附上原始碼

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Google Drive: Copy/Duplicate Folder Structure and Contents without using Desktop App
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


For instructions, go to http://techawakening.org/?p=2846
For queries, bugs reporting comment on the above article.

Written by Shunmugha Sundaram for Techawakening.org - April 27, 2014

--.-- | | o
|,---.,---.|---.,---.. . .,---.|__/ ,---.,---..,---.,---. ,---.,---.,---.
||---'| | |,---|| | |,---|| \ |---'| ||| || | | || | |
``---'`---'` '`---^`-'-'`---^` ``---'` '`` '`---|o`---'` `---|
`---' `---'

Credits:

Finding Subordinate Folders/Files: By David French via http://goo.gl/mhykXU

Change Log:

Jan-13-2015: V1.1: Update to Prevent Files from being Tagged to My Drive Root.

Apr-29-2015: V2: DocList deprecated, Script rewritten to use DriveApp.

To Donate: http://techawakening.org/go/donate
*/

var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var scriptproperties = PropertiesService.getScriptProperties();

function authorize() {
spreadsheet.toast("Enter Folder ID and Select GDrive: Copy Folder-> Make a Copy", "", -1);

}


function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{
name: "1. Authorize",
functionName: "authorize"
}, {
name: "2. Make a Copy",
functionName: "findRootFolder"
}

];
ss.addMenu("GDrive: Copy Folder", menuEntries);
spreadsheet.toast("Select GDrive: Copy Folder-> Authorize. This is an One Time Action.", "Get Started", -1);
}

function findRootFolder() {
var folderId = sheet.getRange("B5").getValue();
var folderId = folderId.toString().trim();
var start = new Date();

try {

var topFolder = DriveApp.getFolderById(folderId);

deletekeys();
spreadsheet.toast("Copy Process Has Started. Please Wait...", "Started", -1);
getFolders_(topFolder.getName(), topFolder);
spreadsheet.toast("Folder Has Been Copied Successfully. Please Check Your Google Drive Now.", "Success", -1);
} catch (e) {
Browser.msgBox("Error", "Sorry, Error Occured: " + e.toString(), Browser.Buttons.OK);
spreadsheet.toast("Error Occurred :( Please make sure you Entered Folder ID in B5 Cell.", "Oops!", -1);
}

}

function getFolders_(path, container) {

var folders = container.getFolders();

var count = 0;
while (folders.hasNext()) {
count++;
var folder = folders.next();
}

var folderCount = count;
var fileslist = container.getFiles();
var fileCountFind = 0;
while (fileslist.hasNext()) {
fileCountFind++;
var file = fileslist.next();
Logger.log(file.getName());
}
var fileCount = fileCountFind;
var files = container.getFiles();

Logger.log("container.getName() " + container.getName() + "| folder length " + folderCount + "| files length " + fileCount);

if (folderCount <= 0) {

if (fileCount > 0) {
Logger.log("Just Files Found Loop");
copyfiles_(container.getName(), files);
}
}

if (folderCount) {
Logger.log("If loop, Folders Found");
var folders = container.getFolders();

while (folders.hasNext()) {

var folder = folders.next();
copy_(container.getName(), folder.getName(), files);
var thisFolder = folder.getName();
var thisPath = path + "/" + thisFolder;
Logger.log("Folder Name:" + folder.getName());
getFolders_(thisPath, folder);

}

}
return;
}

function copy_(containername, childname, files)

{

if (scriptproperties.getProperty(containername + "copy")) {
Logger.log("if");
var parentcontainer = DriveApp.getFolderById(scriptproperties.getProperty(containername + "copy"));

if (!scriptproperties.getProperty(containername + "processed")) {
/*process, copy files*/

while (files.hasNext()) {
var file = files.next();
var newFile = file.makeCopy(file.getName(), parentcontainer);
//parentcontainer.addFile(newFile);
DriveApp.getRootFolder().removeFile(newFile);
Utilities.sleep(2000);
}




}

scriptproperties.setProperty(containername + "processed", "true");
var childfolder = parentcontainer.createFolder(childname + "_copy");
var childfolderid = childfolder.getId();
scriptproperties.setProperty(childname + "copy", childfolderid);

} else {

Logger.log("else");
var parentfold = DriveApp.createFolder(containername + "_copy");
var parentfoldid = parentfold.getId();
scriptproperties.setProperty(containername + "copy", parentfoldid);



if (!scriptproperties.getProperty(containername + "processed")) {
/*process, copy files*/

while (files.hasNext()) {
var file = files.next();
var newFile = file.makeCopy(file.getName(), parentfold);
// parentfold.addFile(newFile);
DriveApp.getRootFolder().removeFile(newFile);
Utilities.sleep(2000);
}

}

scriptproperties.setProperty(containername + "processed", "true");
var childfolder = parentfold.createFolder(childname + "_copy");
var childfolderid = childfolder.getId();
scriptproperties.setProperty(childname + "copy", childfolderid);

}
Logger.log("Container Name: " + containername + "||" + " Child Name: " + childname);
}

function copyfiles_(containername, files) {
if (scriptproperties.getProperty(containername + "copy")) {
Logger.log("If Loop, Just files found- down");
var parentcontainer = DriveApp.getFolderById(scriptproperties.getProperty(containername + "copy"));

if (!scriptproperties.getProperty(containername + "processed")) {

while (files.hasNext()) {
var file = files.next();
var newFile = file.makeCopy(file.getName(), parentcontainer);
//parentcontainer.addFile(newFile);
DriveApp.getRootFolder().removeFile(newFile);
Utilities.sleep(2000);
}
}
scriptproperties.setProperty(containername + "processed", "true");

} else {

Logger.log("Else Loop, Just files found");
var parentfold = DriveApp.createFolder(containername + "_copy");
var parentfoldid = parentfold.getId();
scriptproperties.setProperty(containername + "copy", parentfoldid);

if (!scriptproperties.getProperty(containername + "processed")) {

while (files.hasNext()) {
var file = files.next();
var newFile = file.makeCopy(file.getName(), parentfold);
//parentfold.addFile(newFile);
DriveApp.getRootFolder().removeFile(newFile);
Utilities.sleep(2000);
}



}
scriptproperties.setProperty(containername + "processed", "true");
}

}

function deletekeys() {
scriptproperties.deleteAllProperties();
}