Good evening,
I am trying to figure out a way to automatically delete files with duplicate filenames from a Google Drive folder. I’d like to keep the file with the oldest creation date, treat the others as duplicates, and delete the duplicates.
I feel close to achieving this using the below AppScript code (from this site: https://hackernoon.com/how-to-find-and-delete-duplicate-files-in-google-drive). The only issue is the code is seemingly keeping the newest file instead of the oldest one. Do you know what I can do to shift this code to keep the oldest file instead of the newest one? If you have other code in mind to achieve my goal, please also share that.
Thank you!
const FOLDER_ID = "INSERTIDHERE";
// Add id of the folder to check for duplicate
/*
* Function looks for duplicate file names in designated folder and removes them.
* @param {String} fileName
*/
function removeDuplicateFile() {
let folder = DriveApp.getFolderById(FOLDER_ID);
let files = folder.getFiles();
let fileList = [];
// if no file is found return null
if (!files.hasNext()) {
return;
}
// else
while (files.hasNext()) {
let file = files.next(),
name = file.getName(),
size = file.getSize();
// checking this way always leaves first file not deleted
if (isDuplicateFile(fileList, name, size)) {
file.setTrashed(true);
} else {
fileList.push([name, size]);
}
}
}
/*
* Function is helper function of removeDuplicateFile function.
* It checks if theres already a file in the given lst with same name and size and returns true or false
* @param {List} lst
* @param {String} name
* @param {Number} size
* @returns {Boolean}
*/
function isDuplicateFile(lst, name, size) {
for (let i = 0; i < lst.length; i++) {
if (lst[i][0] === name && lst[i][1] === size) return true;
}
return false;
}
/*
* Delete all the triggers if there are any
*/
var deleteTrigger = () => {
let triggersCollection = ScriptApp.getProjectTriggers();
if (triggersCollection.length <= 0) {
console.log(`Event doesnot have trigger id`);
} else {
triggersCollection.forEach((trigger) => ScriptApp.deleteTrigger(trigger));
}
return;
};
/*
* Create a trigger function for file which also deletes previous triggers if there are.
*/
function removeDuplicateFileTrigger() {
// First Delete existing triggers
deleteTrigger();
// now remove duplicate files
removeDuplicateFile();
}
3
Answers
I believe your goal is as follows.
In your script, the script for checking the created date is not included. So, in this case, how about the following modification? In this modification, the function
removeDuplicateFile()
is modified as follows.Modified script:
isDuplicateFile
is not used.References:
You may need to enable Drive API
Alternative Solution to retain the oldest file and delete duplicate files in Google Drive
Please note to enable Drive API.
Reference: find duplicate files in one folder and removing the oldest one? (Google Script)