-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategorize Files.gs
More file actions
59 lines (49 loc) · 2.34 KB
/
Categorize Files.gs
File metadata and controls
59 lines (49 loc) · 2.34 KB
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
// The scan_folder is the folder where the script pull files to categorize.
// Remove the "//" before one of the following to set the scan folder.
// Notice: The script will no look at the subfolders.
//const scan_folder = DriveApp.getRootFolder(); // Scan the root folder
//const scan_folder = DriveApp.getFolderById("1s4rlgOcJLR9PnWckV6Y0ItYFXC3knlv5"); // Scan a custom folder (such as an Inbox folder). ID is the random string at the end of the URL of a folder.
// Here is where you tell the script what you want to put where
// Please adjust it to your need
const folderId = {
"Example-1": "1gv0x14ncUljJRyY4rZITrC5ozkbclcVi",
"Example-2": "1gv0x14ncUljJRyY4rZITrC5ozkbclcVi"
}
const separator = "_"; // The character that separate the label from the file name
// In the example above, a file name "Example-1_I am a File.pdf" will be move to the folder with ID 1gv0x14ncUljJRyY4rZITrC5ozkbclcVi.
// This is optional. If used, a super category will be used when category is not found.
// For example, when no folder for "Lable-A" is found, but there is a folder for "Lable" it use it instead.
const super_separator = "-";
// Do not touch the following
function categorizingFilesMain() {
var files = scan_folder.getFiles();
while(files.hasNext()) {
file = files.next()
console.log("Processing file " + file.getName())
categorizeFile(file)
}
}
function categorizeFile(file) {
const fileName = file.getName();
const slideLocation = fileName.indexOf(separator);
const slideLocation2 = fileName.indexOf(super_separator,3);
if (slideLocation === -1) {return}
const category = fileName.slice(0,slideLocation);
const superCategory = fileName.slice(0,slideLocation2);
if (category in folderId) {
try{
file.moveTo(DriveApp.getFolderById(folderId[category]))
console.log("Moved file " + fileName + " to folder " + category + ".")
} catch(e) {
console.log("An error occured when moving file " + fileName + " to folder " + category + ".\nError message: " + e)
}
} else if (superCategory in folderId) {
try{
file.moveTo(DriveApp.getFolderById(folderId[superCategory]))
console.log("Moved file " + fileName + " to folder " + superCategory + ".")
} catch(e) {
console.log("An error occured when moving file " + fileName + " to folder " + superCategory + ".\nError message: " + e)
}
}
return
}