Automator Not Properly Filtering Files Modified Within Last 7 Days
Asked Answered
C

2

6

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?

Celie answered 23/4, 2018 at 5:3 Comment(1)
What is the output of find . -type d | wc -l and find . -type f | wc -l in the folder?Wachtel
S
4

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.

Signesignet answered 3/7, 2018 at 19:9 Comment(5)
I appreciate the input, the biggest issue I have with this is, I want to retain the folder structure, which is why my Automator flow is such a disaster. For instance, I might have multiple index.html files from various folders and if they are all copied into the same backup folder I won't know where they belong without actually opening the file.Celie
but this code supports folder structure. It copies temp/some/project/index.html to backup/some/project/index.htmlSignesignet
if i misunderstood you, let me correct it. Do you mean you need to see which files are being synced?Signesignet
Oh you know what, I read the code wrong. Let me try running this script. Thanks for the help. I think you're right and I think this is going to work great. The only thing I would want to change is to have it place the backup in a folder that is whatever the current date is in a number format separated by a dash, so todays folder would be 7-9. Ill run this and Ill just have to look into doing that. Thank you for the help!!!Celie
Your welcome. Also you can try to add this bit; date +%-m-%-d-%y to your code for setting the date as you now needed. I haven't tested it yet. Though I can help you with that later.Signesignet
S
3

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
Sapindaceous answered 28/6, 2018 at 0:10 Comment(5)
Sorry for the delayed response, so this will retain the folder structure? For instance if I have 2 files in Folder1, and File 1 is to be kept and File2 is to be deleted, File 2 will be deleted, and the File1 will stay along with the folder?Celie
That was my intention :-). Put the script to the test and see how you like itSapindaceous
Awesome! Thank you. I'll have to give this another try. Since it literally loops through everything and there are like 28,000 files it was taking a minute, but it's going to be a scheduled task that happens while I am asleep, so that won't matter. I'll give it another test run. Thanks!Celie
Going to edit the script because if you were using that many files… Finder app is not the most efficient way to find filesSapindaceous
oh thanks! I really appreciate that. I did test it on a smaller folder and it worked like a charm, the only thing I would like to change is to not have to choose the folders and to just tell it to select the Downloads folder on it's own and then the backup folder I was taking the current Month and day in number order and creating a new folder, so it would create a new folder 7-9 and copy the files to that folder. Thanks for all the help!!!!!Celie

© 2022 - 2024 — McMap. All rights reserved.