Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function deleteFilesOlderThan26Hours() {
- var folder = DriveApp.getFolderById("FOLDER-ID");
- var files = folder.getFiles(); // Get all files in the folder
- var now = new Date(); // Current time
- var hoursLimit = 26; // Time limit in hours (26 hours)
- while (files.hasNext()) {
- var file = files.next();
- var lastUpdated = file.getLastUpdated(); // Get the last updated date of the file
- var diffHours = (now - lastUpdated) / (1000 * 60 * 60); // Calculate the difference in hours
- // Log file details for debugging
- Logger.log("File: " + file.getName());
- Logger.log("Last Updated: " + lastUpdated);
- Logger.log("Age (hours): " + diffHours);
- if (diffHours > hoursLimit) {
- Logger.log("Moving file to Trash: " + file.getName());
- file.setTrashed(true); // Move the file to Trash
- } else {
- Logger.log("File not old enough for deletion: " + file.getName());
- }
- }
- // Permanently empty Trash
- try {
- Drive.Files.emptyTrash(); // Requires Drive API
- Logger.log("Trash has been emptied!");
- } catch (error) {
- Logger.log("Failed to empty Trash: " + error.message);
- }
- Logger.log("File processing complete.");
- }
Advertisement
Comments
-
- This script running on Google apps Script https://script.google.com/
- Created by t.me/mase89
Add Comment
Please, Sign In to add comment
Advertisement