Skip to content

Instantly share code, notes, and snippets.

@CraftCanna
Forked from gunar/code.js
Created August 2, 2024 07:05
Show Gist options
  • Save CraftCanna/33ca395dae7513d184aef904bc211af5 to your computer and use it in GitHub Desktop.
Save CraftCanna/33ca395dae7513d184aef904bc211af5 to your computer and use it in GitHub Desktop.
Remove Duplicates From GDrive Root
/*
Gunar C. Gessner
@gunar 2016
INSTALL: https://script.google.com/macros/s/AKfycbym7IaTORzJ7LQPcxMx1LV1SQEC6RVGv5tzLOgYS8iQe8XAJxM/exec
After installation, the script will, every 10 minutes, search for all files that have duplicates (shift+z) and remove them from the root directory ("My Drive").
This Google Apps Script webapp aims to solve the problem that when transferring ownership of folders &
files to another account, duplicate files shown in "My Drive".
Original Google Drive Help forum issue: https://productforums.google.com/forum/#!topic/drive/Ldz7nvSXQ6w
*/
'use strict'
function updateTriggers(triggerId) {
// Remove all project Triggers
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
ScriptApp.deleteTrigger(allTriggers[i]);
}
// Add single one
ScriptApp.newTrigger('removeDuplicatesFromRoot')
.timeBased()
.everyMinutes(10)
.create();
}
function doGet() {
updateTriggers();
return HtmlService.createHtmlOutput('Script successfully installed! It will remove duplicates every 10 minutes.');
}
function removeDuplicatesFromRoot() {
Logger.clear();
var rootFolder = DriveApp.getRootFolder();
var files = rootFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
var parents = file.getParents();
var isInMyDrive = false;
var isSomewhereElse = false;
while (parents.hasNext()) {
var folder = parents.next();
if (folder == 'My Drive') { isInMyDrive = true }
else { isSomewhereElse = true }
}
Logger.log('===' + file + ': ' + isInMyDrive + '/' + isSomewhereElse);
if (isInMyDrive && isSomewhereElse) { rootFolder.removeFile(file); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment