I have the following very simple macro. It makes a copy every day with the format: 2025-10-31, 20:00 - Test. This works well, but now I want the file created 10 days ago to be deleted (automatically). How should I do this?
Test.kmmacros (2.4 KB)
I have the following very simple macro. It makes a copy every day with the format: 2025-10-31, 20:00 - Test. This works well, but now I want the file created 10 days ago to be deleted (automatically). How should I do this?
Test.kmmacros (2.4 KB)
Bear in mind that there are a couple of ways in which the creation date of each file can be checked, under your system: there is the date that you have embedded in the filename, but also MacOS records the creation date as a metadata attribute for the file. In general, it is better to check the metadata attribute (although I don’t suppose the precise time that the deletion will be carried out will be critical for your needs in this case).
So, on that basis, there are two solutions, that you can modify, in this thread, :
Compare Two Dates - Delete Files Older Than XX Days From Today
Use ICUDateTimeMinus token.
ICUDateTimeMinus%10%Days%yyyy-…%
Yes, I see that the first of the solutions that I linked to takes the approach of calculating the number of seconds but using that token is of course a simpler approach.
I am curious: was that token introduced after the topic that I linked to? I was aware of that token, but when it comes to time-and-date issues in computing, I prefer not to think for myself so much but to put my faith into those who seem to have already worked everything out. ![]()
I suggest you delete every file created more than 9 days ago instead -- that prevents old files from hanging around if your macro doesn't run for some reason.
I'm making an assumption that there could be other files in the folder that you don't want to delete but that they won't start with a date and time.
Since @kevin's already promoted the metadata method let's leverage your sensibly-chosen, ASCII-sorted is also time-sorted, date format instead:
Delete old dated files.kmmacros (4.9 KB)
There are probably terser ways to get the job done, like running this terminal command in an Execute Shell script action that's triggered occasionally, perhaps daily:
find "/path/to/folder" -type f -mtime +10 -name "*.log" -print
I specifically used the option "print" instead of "delete" in case anyone was naive enough to try this command without understanding what it does. Notice that the "name" flag allows the user to specify a regex, and you can use that to restrict the filename patterns.
Thanks, that's what I was looking for. I made a small change and replaced the line '^\d{4}-\d{2}-\d{2}, \d{2}:\d{2}' with '^\d{4}-\d{2}-\d{2}, 20:00'.
This will only delete files created at 20:00. These are automatically created files. This way, files I created using a hotkey will still exist.
Yes, my first thought was a shell find too. But, as I said, I thought it would be fun to do something that relied solely on the file names.
I think you'd also have to include -depth 1 to stop folder traversal.
I've never quite got my head around -mtime -- would +10 only match a last modified of 10 days a go, or is it 10 or more days ago?
I'm sure you are right. But it didn't sound like this user had subfolders. And it may not be a big deal if the user uses a regex to select files that match a certain naming pattern.
I know the user specified 9 days, but I was showing 10 specifically so that the user would take ownership of finding the correct value.
Answering myself -- yes, it's 10 or more days ago.
You could also use -Btime instead of -mtime to search on file creation (B for "birth") date.
The bummer with that is that the macOS built-in find doesn't take POSIX character classes or their shortcuts -- neither [:digit:] nor \d will work, so we have to use [0-9].
Putting all that together with @Seagram's expanded pattern:
find -E . -depth 1 -type f -Btime 10 -regex ".*/[0-9]{4}-[0-9]{2}-[0-9]{2}, 20:00.*" -print
That was fun! Thanks for nudge, @Airy.
It was added in version 5.2 in April 2012.
After some experimentation and using chatGPT, I finally created the following macro. If anyone has any comments or suggestions for improvement, please let me know.
Backup.kmmacros (6.1 KB)
Save your date/time stamp into a variable, then use that variable in your "Copy File" and "Set Variable" actions.
There's a very small, but still non-zero, chance that the file copy takes more than a minute -- if it does your two paths won't match.
Not so important here, but a good habit to get into -- if you want to use a single derived value in various places in a macro then get it once, store it in a variable, then reference that variable throughout.
Oh, and the SetFile command was deprecated 11 years ago
It's still hanging around but you should start looking for an alternative, just in case... Do you actually need to set the creation date of the file? If not, the touch utility is pretty much the standard for changing modification dates.
But why spin up a shell at all? See if a pair of KM "Set File Attribute" actions will do what you want.
How would I do that with "set file attribute" in my macro? If I want to change both creation and modification to the current date/time?
Yes, I have a comment. Everything you are doing here, and more, is already available to you in an app on macOS called Time Machine. You can start Time Machine from the System Settings app (in the "General" pane.) Here is its description:
Time Machine
Time Machine backs up your computer and keeps local snapshots and
hourly backups for the past 24 hours, daily backups for the past month
and weekly backups for all previous months. The oldest backups and any
local snapshots are deleted as space is needed.
You're right but as they say 1 backup is no backup.
Also, I don't think TM is the most user-friendly program right now.
Maybe. But it’s a lot more user-friendly than using macOS shell commands.
But that's just a one-time deal. Once you've figured that out, you're done. No more hassle with TM.
Thanks for the tip
Removed the shell action and used the set file attribute here. And only used the modified date. Thanks for the tips.