Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 956
mcbeav Avatar asked Apr 23 '18 05:04

mcbeav


People also ask

How to get last modified data from last 5 minutes?

You need to use GetMetadata activity with a dataset pointing to your folder (2021/06/25) and use filter by Last modified values for last 5 minutes and use Child items field. This will help you child items which are modified in last 5 minutes.

How to get last 5 minutes of activity in metadata?

Step1: GetMetaData Activity to take files which are modified in last 5 minutes from data folder Step3: IF Activity to check "Employees.csv" file exists or not Step4: IF fail exists then under True use your activities accordingly. Hope this will help.

How do I run a flow without a filter?

1) Run your Flow without the filter and take a look at the output of the Get items action. You need to refer to the column in exactly the same way as it is shown in the output.

Why am I not getting any items in my filter query?

If you set the Filter Query like this, I think that even if the formula is correct, you may not get any items. Because Modfied is definitely less than the time at the moment. Could you provide more details about it? Anyway, a similar formula should look like this: Take a try with your actual needs and feel free let me know if you have any issue.


2 Answers

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.

like image 58
pegasuspect Avatar answered Oct 17 '22 15:10

pegasuspect


Here is an entire AppleScript solution that you can save this code as an app in Script Editor

set mainFolder to choose folder with prompt "PLEASE CHOOSE YOUR SOURCE FOLDER TO BACKUP"
set backupFolder to choose folder with prompt "PLEASE CHOOSE YOUR BACKUP DESTINATION FOLDER"
set theDate to (current date) - (7 * days)

tell application "Finder"
    set allFiles to entire contents of folder mainFolder as alias list
    try
        duplicate allFiles to folder backupFolder
    end try
    set allFiles2 to every file of entire contents of folder backupFolder
end tell

repeat with i from 1 to count of allFiles2
    set thisFile to item i of allFiles2
    if modification date of thisFile is less than theDate then
        tell application "Finder"
            delete thisFile
        end tell
    end if
end repeat
like image 34
wch1zpink Avatar answered Oct 17 '22 14:10

wch1zpink