You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Automator无法正确筛选近7天修改文件的技术咨询

Fixing Automator Backup: Retain Folder Structure & Keep Only 7-Day Modified Files

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:

  1. 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).
  2. Add the Select Folder action (or Get Specified Finder Items if you want a fixed source directory) to choose your dev files folder.
  3. Add the Run Shell Script action. Set the shell to /bin/bash and set "Pass input" to as arguments.
  4. Paste this script into the text box (update the BACKUP_DIR path 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 (+7 would 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-up index.js files!).
  • 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:

  1. Start with a Get Specified Finder Items action pointing to your dev directory.
  2. 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.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:57:13