-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.gs
33 lines (29 loc) · 1.07 KB
/
code.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
function checkForNewFiles() {
// Array containing IDs of all folders where files should be renamed
// Replace "<folderID...>" with the IDs of your Google Drive folders.
// Get IDs by copying the share url and use string between "drive.google.com/drive/folders/" and "?"
var folderIds = [
"<folderID1>",
"<folderID2>",
"<folderID3>"
];
for (var i = 0; i < folderIds.length; i++) {
var folderId = folderIds[i];
processFolder(folderId);
}
}
function processFolder(folderId) {
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFiles();
var currentDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyyMMdd-HHmm');
// Regular expression to match any date in 'yyyyMMdd' format followed by an underscore,
// so files that already have at least the date as prefix won't be renamed
var regex = new RegExp("^\\d{8}(_|-)");
while (files.hasNext()) {
var file = files.next();
if (!regex.test(file.getName())) {
var newName = currentDate + "_" + file.getName();
file.setName(newName);
}
}
}