Automator无法正确筛选近7天修改文件的技术咨询
Hey there! Let’s sort out this Automator backup snag you’re dealing with. Keeping your folder structure intact while only hanging onto files modified in the last 7 days is totally achievable—Automator can do this, but it often needs a little help from shell scripting to avoid common pitfalls like misfiltering files or losing directory hierarchy.
Why Your Current Automator Setup Might Be Failing
Chances are, if you’re using the built-in Filter Finder Items action alone, it’s not recursively scanning subdirectories properly, or it’s trying to delete folders along with unmodified files. Automator’s native actions can be finicky when you need to preserve structure while targeting specific files.
Solution 1: Automator + Shell Script (Most Reliable)
This approach uses a bash script to handle the file filtering and structure preservation—way more consistent for complex backup tasks. Here’s how to build it:
- Open Automator and create a new Application (for manual runs) or Folder Action (if you want it to trigger when files are added to a folder).
- Add the Select Folder action (or Get Specified Finder Items if you want a fixed source directory) to choose your dev files folder.
- Add the Run Shell Script action. Set the shell to
/bin/bashand set "Pass input" to as arguments. - Paste this script into the text box (update the
BACKUP_DIRpath to your preferred backup location):
# Define source and backup directories SOURCE_DIR="$1" # Create a date-named backup folder to avoid overwrites BACKUP_DIR="/Users/YourName/DevBackups/backup_$(date +%Y-%m-%d)" # Create the backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" # Recursively copy only files modified in the last 7 days, preserving folder structure find "$SOURCE_DIR" -type f -mtime -7 -exec cp -a --parents {} "$BACKUP_DIR" \; # Optional: Delete unmodified files from the source directory (TEST FIRST!) # Uncomment below only after verifying the backup works correctly # find "$SOURCE_DIR" -type f -mtime +7 -delete
Script Breakdown:
-mtime -7: Targets files modified within the last 7 days (+7would target files older than 7 days).cp -a --parents: Copies files with their full relative path, so your folder structure stays exactly as it was (no more mixed-upindex.jsfiles!).- The date-named backup folder ensures you don’t overwrite previous weekly backups.
Solution 2: Pure Automator Workflow (No Scripting)
If you’d rather stick to Automator’s native actions, tweak your setup like this:
- Start with a Get Specified Finder Items action pointing to your dev directory.
- Add a Find Finder Items action:
- Set the condition to
Date Modified→is in the last→7 days. - Check the Recursively box at the bottom to scan all subdirectories.
- Set the condition to
- Add a Copy Finder Items action. Select your backup directory, then click the Options button and check Preserve folder structure.
⚠️ Heads Up: This method works for copying, but if you want to delete unmodified files from the source, be extra careful. Automator’s Move to Trash action can accidentally delete folders if you’re not precise—always test with a copy first.
Pro Tips
- Test First: Always run the workflow on a test copy of your dev files before using it on your actual work.
- Schedule It: To automate weekly runs, save your Automator app, then open the Calendar app, create a weekly event, and add an "Open file" alert pointing to your Automator app.
- Avoid Accidental Deletion: If you’re deleting old files, consider keeping a separate archive of monthly backups just in case.
内容的提问来源于stack exchange,提问作者mcbeav

