-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleanFolderName-cp.js
64 lines (52 loc) · 2.78 KB
/
CleanFolderName-cp.js
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
/*
Script Title: Google Housekeeping - Clean Folder Names
Version: 1.0
Creation Date: 5/22/2020
Author: Robert C. Tucker
Description: The Google Drive Housekeeping - Clean Folder Names script is a google script designed to cull through a folder on Google Drive and remove punctuation from Folder Names.
Notes: This Script must be run at script.google.com, unless you have a standard hosted google project with drive api access.
*/
function CleanFolderNames (folder) {
//Declare all variables.
var SearchFolder = "";
var FolderID = "";
var FolderName = "";
//Replace FolderIDs iin arrSearchFolderIDs with IDs of the folders you wish to search within. This can be obtained by going to your desired folder and copying everything in the URL after https://drive.google.com/drive/folders/
var arrSearchFolderIDs = [
'<FolderID>',
'<FolderID>'
];
for (var i = 0; i < arrSearchFolderIDs.length; i++) {
SearchFolder = DriveApp.getFolderById(arrSearchFolderIDs[i]);
var SearchFolderName = SearchFolder.getName();
arrSearchFolders.push(SearchFolderName);
//Set Folder as a variable containing folders within search folder.
var Folders = SearchFolder.getFolders();
//While statment to search for all folders within the search folder.
while (Folders.hasNext()) {
var Folder = Folders.next();
FolderID = Folder.getId();
FolderName = Folder.getName();
//Replace Templates with folders you would like the script to ignore and not remove.
//Put the ID of each Folder into the arrFolderIDs array and the Name for each folder into the FolderNames Array.
if (FolderName != "Templates"){
var hasFolder = Folder.getFolders().hasNext(); // get the list of files in the root of that folder
// Logger.log ('\n' + "Search Folder: " + FolderName)
//Determine if a Folder has child Folders or not. If not, Check Folder name for periods and rename folder if periods found.
if (!hasFolder) {
Logger.log ('\n' + "Folder name is: " + FolderName)
var strCleanName = FolderName.replace(/[.]/g, "");
Folder.setName(strCleanName)
FolderName = Folder.getName();
Logger.log ('\n' + FolderName + " Has No Child Folders and has had all . removed from name.(CleanFolderNames)" + '\n')
}
//If Folder has child folder add folder ID to the arrSearchFolderIDs array and continue.
if (hasFolder){
Logger.log ('\n' + FolderName + "Folder has child folders. Added to Search Array")
arrSearchFolderIDs.push(FolderID);
}
}
}
}
return arrSearchFolderIDs
}