Search code examples
macosfilesystemsautomator

Automator Not Properly Filtering Files Modified Within Last 7 Days


I am trying to automate the process of backing up my development files weekly. I want to automatically back up the files, filter through all of the files, and delete the files that have not been modified within the last 7 days, but retain the folder structure. For example, I will have many files named index.js, so in order to know where they belong, I would like the folder structure to be maintained and the file to stay in it's proper folder if it has been modified within the last 7 days. My process so far has been to

  • Copy the whole folder containing all development files

Automator settings showing the first step I have set is to Get Specified Finder Items which is the folder I want to copy and then to Copy Finder Items to the backup folder

This works no problem. It copies all 29K files. The next step is to get all of the content inside of the folder and filter it down.

  • Get Specified Finder Items ( The folder that was just copied )

  • Get Folder Contents ( Repeat for each subfolder found )

29,303 Items inside of the folder that has been copied

Right off the bat, I have an issue. When Getting Folder Contents and checking Repeat For Each Subfolder Found around 8,000 files are missing.

Right off the bat, I have an issue. When Getting Folder Contents and checking Repeat For Each Subfolder Found around 8,000 files are missing. I can't seem to figure out what is causing this or how to fix this.

If I ignore this and continue on, my next step is to filter down the items. I want to remove the items not modified within the last 7 days and since I want to retain the folder structure I want to make sure the item is not a folder

  • Filter Finder Items
  • is set to find files where the date last modified is not in the last 7 days ( this should grab all of the files that have not been modified recently )

  • Filter Finder Items

  • now I filter through those results and make sure to grab all of the same files but no folders

  • Move Finder Items To Trash

  • lastly these items that have not been modified within the last 7 days and are not folders are moved to the trash.

So if I ignore the initial 8,000 files missing when Automator initially gets the folder's contents, and run this it does not work as intended. It does delete a lot of files, but a ton of files that have not been modified within the past 7 days are left, and I can not figure out why some files are being deleted and some are being left. I'm not sure if there is a flaw in my process or what I am missing here. Can someone point me in the correct direction for what I am trying to achieve?


Solution

  • Paste the code here

    if [[ ! -d ~/Downloads/backup ]]; then
      mkdir ~/Downloads/backup
    fi
    cd ~/Downloads/temp/
    find . -type f -newermt '7 days ago' -exec rsync -R {} ~/Downloads/backup \;
    # You can change this -----^^^^ to hours as well!
    

    Add the above code as seen in the picture. You are done!

    How the Code Works:

    First of all I did it other way around. In your workflow you copy all of the files then you filter them. In this workflow I filtered the files first then copy them.

    First line of the code basically checks if there is not a backup directory in Downloads of your home directory.

    Second line; creates backup folder if first line is true.

    Third line ends this check.

    4th line finds all the files which have a modified time (-newermt) within the last 7 days ('7 days ago'). For each file matching this criteria I used rsync to copy them to ~/Downloads/backup, which is default path to your backup folder.