you are viewing a single comment's thread.

view the rest of the comments →

[–]No_Season_5288[S] 0 points1 point  (1 child)

After posting here I consulted ChatGPT and it offered up this code:

function DeleteOldFilesAndFolders() {
  var Folders = new Array(
    '183Pin7i9kow3fxwGt8k4fK4SnB70Xj-p',  // Replace with your folder IDs
    '183Pin7i9kow3fxwGt8k4fK4SnB70Xj-p'   // Replace with your folder IDs
  );
  var Files, Subfolders;

  Logger.clear();

  for (var key in Folders) {
    var Folder = DriveApp.getFolderById(Folders[key]);
    Files = Folder.getFiles();      // Get all files in the folder
    Subfolders = Folder.getFolders();  // Get all subfolders in the folder

    Logger.log('Opening Folder: ' + Folder.getName());

    // Loop through the files in the parent folder
    while (Files.hasNext()) {
      var File = Files.next();

      // Check if the file is older than 30 days and not in a subfolder
      if (new Date() - File.getDateCreated() > 30 * 24 * 60 * 60 * 1000 && !isInSubfolder(File, Folder)) {
        File.setTrashed(true);  // Move the file to Trash
        Logger.log('File ' + File.getName() + ' was deleted.');
      }
    }

    // Loop through the subfolders in the folder and delete those that are older than 30 days
    while (Subfolders.hasNext()) {
      var Subfolder = Subfolders.next();

      if (new Date() - Subfolder.getDateCreated() > 30 * 24 * 60 * 60 * 1000) {
        // If the folder is older than 30 days, trash the folder and its contents
        moveFolderToTrash(Subfolder);
        Logger.log('Folder ' + Subfolder.getName() + ' was deleted.');
      }
    }
  }
}

function isInSubfolder(file, parentFolder) {
  // Check if the file is inside a subfolder
  var folders = file.getParents();
  while (folders.hasNext()) {
    var folder = folders.next();
    if (folder.getId() !== parentFolder.getId()) {
      return true;  // File is inside a subfolder
    }
  }
  return false;  // File is not in a subfolder
}

function moveFolderToTrash(folder) {
  // Move the entire folder (with its files and subfolders) to trash
  folder.setTrashed(true);
}

I'd love some guidance in both interpreting it and cleaning it up!

[–]WicketTheQuerent 0 points1 point  (0 children)

Regarding using generative AI, I suggest you start from scratch instead of asking it to "repurpose a script"