乐于分享
好东西不私藏

用OpenClaw打造个人文件自动整理助手

用OpenClaw打造个人文件自动整理助手

每天自动分类数千个文件,让你的桌面和下载文件夹永保整洁

一、这个自动化功能能解决什么问题?

你是否经历过这样的场景?下载文件夹里堆满了各种文件——PDF报告、会议截图、压缩包、安装程序,每次找文件都要花好几分钟。桌面图标多到看不清壁纸,不同项目的文档混杂在一起。

文件自动整理助手能帮你:

  • 📁 自动将下载文件按类型分类归档

  • 🖼️ 智能识别截图并自动整理

  • 📄 批量重命名并按日期整理文档

  • ⏰ 定时清理临时文件

  • 📊 生成文件清单报告

  • 🔄 7×24小时后台运行,无需手动操作

二、搭建前的准备工作

2.1 系统与环境要求

✅ 已安装并运行 OpenClaw✅ 系统:macOS / Windows / Linux✅ 磁盘:至少100MB可用空间✅ 权限:有文件读写权限

2.2 检查OpenClaw状态

打开终端,运行以下命令确认环境正常:

# 检查OpenClaw版本openclaw --version# 检查网关服务状态openclaw gateway status# ✅ 应该显示: Gateway is running on http://localhost:18789# 查看已安装的插件openclaw plugins list

三、完整搭建步骤

3.1 创建专用配置文件目录

# 创建文件整理助手的配置目录mkdir -p ~/.openclaw/flows/file-organizercd ~/.openclaw/flows/file-organizer

3.2 编写核心配置文件

创建文件:file-organizer.yaml

# file-organizer.yamlname: 智能文件整理助手description: 自动整理下载文件夹、桌面和指定目录的文件version: 1.0.0author: 你的名字enabled: true# 触发器配置 - 什么时候执行整理triggers:  # 定时触发器:每天9:00和18:00自动执行  - type: schedule    id: morning-cleanup    schedule: "0 9 * * *"    timezone: "Asia/Shanghai"    description: "每天早上9点整理文件"  - type: schedule    id: evening-cleanup    schedule: "0 18 * * *"    timezone: "Asia/Shanghai"    description: "每天下午6点整理文件"  # 文件夹监控触发器:当Downloads有大量新文件时触发  - type: filesystem    id: download-monitor    path: "~/Downloads"    events: ["create", "modify"]    min_events: 5    cooldown: 300    description: "下载文件夹监控"# 任务流程定义tasks:  # 任务1: 创建分类目录结构  - name: 初始化分类目录    description: 创建整理所需的目录结构    actions:      - type: shell        id: create-folders        command: |          # 基础分类目录          mkdir -p ~/Documents/分类整理/{PDF文档,图片截图,视频媒体,压缩文件,办公文档,安装程序,代码项目,其他文件}          # 按时间分组的目录          mkdir -p ~/Documents/分类整理/按时间归档/$(date +%Y)/$(date +%m)/$(date +%d)          # 临时中转目录          mkdir -p ~/Documents/分类整理/待处理/          echo "📁 目录结构创建完成"  # 任务2: 整理下载文件夹  - name: 整理下载文件夹    description: 将Downloads中的文件按类型分类    depends_on: ["初始化分类目录"]    actions:      - type: shell        id: organize-downloads        command: |          # 定义目标路径          PDF_DIR=~/Documents/分类整理/PDF文档          IMAGE_DIR=~/Documents/分类整理/图片截图          VIDEO_DIR=~/Documents/分类整理/视频媒体          ARCHIVE_DIR=~/Documents/分类整理/压缩文件          DOC_DIR=~/Documents/分类整理/办公文档          INSTALLER_DIR=~/Documents/分类整理/安装程序          CODE_DIR=~/Documents/分类整理/代码项目          OTHER_DIR=~/Documents/分类整理/其他文件          TODAY_DIR=~/Documents/分类整理/按时间归档/$(date +%Y)/$(date +%m)/$(date +%d)          # 记录开始时间          echo "⏰ 开始整理下载文件夹: $(date)"          # 处理PDF文件          find ~/Downloads -maxdepth 1 -type f -name "*.pdf" -exec mv {} "$PDF_DIR/" \;          echo "📄 移动了 $(find "$PDF_DIR" -name "*.pdf" -newer ~/Downloads/.last_processed 2>/dev/null | wc -l) 个PDF文件"          # 处理图片文件          find ~/Downloads -maxdepth 1 -type f \( -name "*.jpg" -o -name "*.jpeg" -o -name "*.png" -o -name "*.gif" -o -name "*.bmp" -o -name "*.webp" -o -name "*.svg" \) -exec mv {} "$IMAGE_DIR/" \;          # 处理截图(智能识别)          for file in ~/Downloads/屏幕快照*.png ~/Downloads/Screenshot*.png ~/Downloads/*截图*.png ~/Downloads/*.截屏*.png; do            if [ -f "$file" ]; then              # 按日期重命名截图              filename=$(basename "$file")              timestamp=$(date -r "$file" +%Y%m%d_%H%M%S)              mv "$file" "$IMAGE_DIR/截图_${timestamp}.png"            fi          done          echo "🖼️ 整理了图片文件"          # 处理文档文件          find ~/Downloads -maxdepth 1 -type f \( -name "*.doc" -o -name "*.docx" -o -name "*.xls" -o -name "*.xlsx" -o -name "*.ppt" -o -name "*.pptx" -o -name "*.txt" -o -name "*.md" \) -exec mv {} "$DOC_DIR/" \;          # 处理压缩文件          find ~/Downloads -maxdepth 1 -type f \( -name "*.zip" -o -name "*.rar" -o -name "*.7z" -o -name "*.tar.gz" -o -name "*.tar" -o -name "*.gz" \) -exec mv {} "$ARCHIVE_DIR/" \;          # 处理安装程序          find ~/Downloads -maxdepth 1 -type f \( -name "*.dmg" -o -name "*.pkg" -o -name "*.exe" -o -name "*.msi" -o -name "*.apk" -o -name "*.dmg" \) -exec mv {} "$INSTALLER_DIR/" \;          # 处理代码相关文件          find ~/Downloads -maxdepth 1 -type f \( -name "*.py" -o -name "*.js" -o -name "*.json" -o -name "*.html" -o -name "*.css" -o -name "*.java" -o -name "*.c" -o -name "*.cpp" \) -exec mv {} "$CODE_DIR/" \;          # 处理视频和音频          find ~/Downloads -maxdepth 1 -type f \( -name "*.mp4" -o -name "*.mov" -o -name "*.avi" -o -name "*.mkv" -o -name "*.mp3" -o -name "*.wav" -o -name "*.flac" \) -exec mv {} "$VIDEO_DIR/" \;          # 处理其他文件(移动文件前先复制到今日目录备份)          for file in ~/Downloads/*; do            if [ -f "$file" ]; then              filename=$(basename "$file")              # 如果不是目录,则认为是文件              cp "$file" "$TODAY_DIR/$filename" 2>/dev/null              mv "$file" "$OTHER_DIR/" 2>/dev/null            fi          done          # 清理空目录          find ~/Downloads -type d -empty -delete          echo "✅ 下载文件夹整理完成"      - type: notification        id: notify-downloads-done        title: "📁 下载文件夹整理完成"        message: "已自动分类整理所有下载文件"        level: info  # 任务3: 整理桌面  - name: 整理桌面    description: 整理桌面文件,按项目分类    depends_on: ["初始化分类目录"]    actions:      - type: python        id: organize-desktop        script: |          import os          import shutil          from datetime import datetime          import json          DESKTOP_PATH = os.path.expanduser("~/Desktop")          ORGANIZED_PATH = os.path.expanduser("~/Documents/分类整理")          # 定义分类规则          CATEGORIES = {              "PDF文档": [".pdf"],              "图片截图": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg"],              "办公文档": [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt", ".md"],              "设计文件": [".psd", ".ai", ".sketch", ".fig", ".xd"],              "代码项目": [".py", ".js", ".java", ".cpp", ".c", ".html", ".css", ".json", ".xml"],              "视频媒体": [".mp4", ".mov", ".avi", ".mkv", ".mp3", ".wav", ".flac"],          }          def organize_desktop():              moved_files = {}              for filename in os.listdir(DESKTOP_PATH):                  filepath = os.path.join(DESKTOP_PATH, filename)                  # 跳过目录和系统文件                  if os.path.isdir(filepath) or filename.startswith("."):                      continue                  # 按扩展名分类                  _, ext = os.path.splitext(filename)                  ext = ext.lower()                  target_category = "其他文件"                  for category, extensions in CATEGORIES.items():                      if ext in extensions:                          target_category = category                          break                  # 创建目标目录                  target_dir = os.path.join(ORGANIZED_PATH, target_category)                  os.makedirs(target_dir, exist_ok=True)                  # 处理重名文件                  target_path = os.path.join(target_dir, filename)                  counter = 1                  while os.path.exists(target_path):                      name, ext = os.path.splitext(filename)                      target_path = os.path.join(target_dir, f"{name}_{counter}{ext}")                      counter += 1                  # 移动文件                  shutil.move(filepath, target_path)                  if target_category not in moved_files:                      moved_files[target_category] = []                  moved_files[target_category].append(filename)              # 生成整理报告              report = {                  "整理时间": datetime.now().isoformat(),                  "整理文件数": sum(len(files) for files in moved_files.values()),                  "分类统计": moved_files              }              report_path = os.path.join(ORGANIZED_PATH, f"桌面整理报告_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")              with open(report_path, 'w', encoding='utf-8') as f:                  json.dump(report, f, ensure_ascii=False, indent=2)              return report          if __name__ == "__main__":              result = organize_desktop()              print(f"整理完成: 共整理了 {result['整理文件数']} 个文件")        enabled: true    # 桌面整理完成后发送通知    notifications:      - type: webhook        url: "http://localhost:18789/api/notifications"        method: POST        headers:          Content-Type: application/json        body: |          {            "title": "🖥️ 桌面整理完成",            "message": "已自动整理桌面文件,按项目分类归档",            "timestamp": "{{now}}"          }  # 任务4: 生成整理报告  - name: 生成整理报告    description: 创建详细的文件整理报告    depends_on: ["整理下载文件夹", "整理桌面"]    actions:      - type: shell        id: generate-report        command: |          REPORT_DIR=~/Documents/分类整理/整理报告          mkdir -p "$REPORT_DIR"          # 生成文件统计          echo "# 文件整理报告" > "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"          echo "生成时间: $(date)" >> "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"          echo "" >> "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"          echo "## 📊 文件统计" >> "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"          for dir in ~/Documents/分类整理/*/; do            if [ -d "$dir" ]; then              dirname=$(basename "$dir")              count=$(find "$dir" -type f | wc -l)              echo "- **$dirname**: $count 个文件" >> "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"            fi          done          echo "" >> "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"          echo "## 📁 最新文件" >> "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"          find ~/Documents/分类整理 -type f -mtime -7 -exec ls -lh {} \; | head -20 | while read line; do            echo "- $line" >> "$REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"          done          # 生成JSON格式的报告(供其他系统使用)          echo '{"timestamp":"'$(date -Iseconds)'","total_files":'$(find ~/Documents/分类整理 -type f | wc -l)'}' > "$REPORT_DIR/statistics.json"          echo "📈 报告已生成: $REPORT_DIR/报告_$(date +%Y%m%d_%H%M%S).md"  # 任务5: 清理临时文件  - name: 清理临时文件    description: 清理系统临时文件和缓存    actions:      - type: shell        id: cleanup-temp        command: |          echo "🧹 开始清理临时文件..."          # 清理下载的临时文件(超过30天)          find ~/Downloads -name "*.tmp" -type f -mtime +30 -delete          find ~/Downloads -name "*.temp" -type f -mtime +30 -delete          find ~/Downloads -name "*.crdownload" -type f -mtime +7 -delete          # 清理浏览器缓存(示例,实际路径可能不同)          # rm -rf ~/Library/Caches/Google/Chrome/Default/Cache/*          # rm -rf ~/.cache/google-chrome/*          # 清理系统临时文件          if [ "$(uname)" = "Darwin" ]; then            # macOS            rm -rf ~/Library/Caches/*            rm -rf /private/var/folders/*/*/*/T/*          elif [ "$(uname)" = "Linux" ]; then            # Linux            rm -rf ~/.cache/*            rm -rf /tmp/*          fi          echo "✅ 临时文件清理完成"# 错误处理配置error_handling:  max_retries: 3  retry_delay: 60  on_failure:    - type: notification      title: "❌ 文件整理失败"      message: "文件整理任务执行失败,请检查日志"      level: error# 性能配置performance:  max_concurrent_tasks: 2  timeout: 300

3.3 创建Python辅助脚本

创建文件:desktop_organizer.py(上面的Python代码单独保存)

# 创建Python脚本目录mkdir -p ~/.openclaw/scripts/file-organizer# 将上面任务3的Python代码保存到这个文件cat > ~/.openclaw/scripts/file-organizer/desktop_organizer.py << 'EOF'# desktop_organizer.pyimport osimport shutilfrom datetime import datetimeimport jsonDESKTOP_PATH = os.path.expanduser("~/Desktop")ORGANIZED_PATH = os.path.expanduser("~/Documents/分类整理")# ... [完整Python代码,同上面的任务3]EOF

3.4 安装流程到OpenClaw

# 1. 上传配置文件到OpenClawopenclaw flows create file-organizer.yaml# 2. 启用这个流程openclaw flows enable "智能文件整理助手"# 3. 立即测试运行一次openclaw flows run "智能文件整理助手"# 4. 查看执行状态openclaw flows status "智能文件整理助手"

3.5 配置通知(可选)

如果你想在整理完成后收到通知,可以配置通知渠道:

# 配置桌面通知(macOS)openclaw notifications add \  --type desktop \  --name "文件整理通知"# 配置Webhook通知(如钉钉、飞书)openclaw notifications add \  --type webhook \  --name "钉钉通知" \  --url "https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN" \  --method POST \  --headers '{"Content-Type": "application/json"}' \  --template '{"msgtype": "text", "text": {"content": "{{title}}\n{{message}}"}}'

四、测试与验证

4.1 手动触发测试

# 创建一些测试文件mkdir -p ~/Downloads/test_filescd ~/Downloads/test_filestouch {document1,document2}.pdftouch {screenshot1,photo1}.pngtouch data.ziptouch notes.txttouch video.mp4touch script.py# 手动运行整理流程openclaw flows run "智能文件整理助手" --task "整理下载文件夹"# 查看执行日志openclaw flows logs "智能文件整理助手" --tail 20

4.2 验证整理结果

# 查看整理后的文件结构tree ~/Documents/分类整理 -L 3# 查看各分类文件数量find ~/Documents/分类整理 -type f | grep -c "\.pdf$"find ~/Documents/分类整理 -type f | grep -c "\.png$\|\.jpg$\|\.jpeg$"find ~/Documents/分类整理 -type f | grep -c "\.zip$\|\.tar.gz$"# 查看生成的报告ls -la ~/Documents/分类整理/整理报告/cat ~/Documents/分类整理/整理报告/报告_*.md | head -20

4.3 查看定时任务状态

# 查看所有定时任务openclaw scheduler list# 查看特定触发器的下次执行时间openclaw scheduler info "智能文件整理助手.morning-cleanup"

你可以通过 openclaw flows logs "智能文件整理助手"随时查看运行日志,或手动运行 openclaw flows run "智能文件整理助手"立即执行整理。

回首页,搜索你想要的文档

在首页搜索框输入关键词,快速定位资料

文库首页搜索指引 立即搜索

相关学习资料