Back to Blog

Automating File Organization with AI Agents

## The Problem: Digital Clutter Your Downloads folder is chaos. Screenshots scattered everywhere. Documents with names like "final_v3_FINAL.docx". Sound familiar? OpenClaw can fix this. Let's build a skill that automatically sorts files based on their content, not just their extension. ## Prerequisites - OpenClaw installed and running - Basic familiarity with OpenClaw skills - A messy folder (you probably have one) ## Step 1: Define Your Organization Rules Before writing code, decide your folder structure: ``` ~/Documents/ ├── Work/ │ ├── Invoices/ │ ├── Reports/ │ └── Presentations/ ├── Personal/ │ ├── Photos/ │ ├── Receipts/ │ └── Medical/ └── Archive/ ``` ## Step 2: Create the File Organizer Skill ```javascript // skills/file-organizer/index.js const fs = require('fs'); const path = require('path'); const RULES = { 'Work/Invoices': ['invoice', 'bill', 'payment', 'receipt'], 'Work/Reports': ['report', 'analysis', 'quarterly', 'annual'], 'Work/Presentations': ['.pptx', '.key', 'slides', 'presentation'], 'Personal/Photos': ['.jpg', '.jpeg', '.png', '.heic'], 'Personal/Receipts': ['receipt', 'order confirmation', 'purchase'], }; function classifyFile(filename, content) { const lower = (filename + ' ' + content).toLowerCase(); for (const [folder, keywords] of Object.entries(RULES)) { if (keywords.some(kw => lower.includes(kw))) return folder; } return 'Archive'; } async function organizeFolder(sourceDir, targetBase) { const files = fs.readdirSync(sourceDir); let moved = 0; for (const file of files) { const src = path.join(sourceDir, file); if (fs.statSync(src).isDirectory()) continue; const dest = classifyFile(file, ''); const targetDir = path.join(targetBase, dest); fs.mkdirSync(targetDir, { recursive: true }); fs.renameSync(src, path.join(targetDir, file)); moved++; console.log(file + ' → ' + dest); } return moved; } module.exports = { organizeFolder }; ``` ## Step 3: Add AI-Powered Content Classification For smarter sorting, let OpenClaw read the file contents: ```javascript // Use OpenClaw's built-in AI to classify documents async function smartClassify(filePath) { const ext = path.extname(filePath).toLowerCase(); // For text-based files, read and classify by content if (['.txt', '.md', '.csv', '.json'].includes(ext)) { const content = fs.readFileSync(filePath, 'utf8').substring(0, 1000); // OpenClaw agent determines the category based on content return classifyFile(path.basename(filePath), content); } // For other files, classify by filename and extension return classifyFile(path.basename(filePath), ''); } ``` ## Step 4: Schedule Automatic Cleanup Add to your OpenClaw cron: ```javascript // Run every day at midnight { schedule: { kind: 'cron', expr: '0 0 * * *' }, payload: { kind: 'agentTurn', message: 'Organize my Downloads folder' } } ``` ## Troubleshooting **Files not moving?** Check permissions: `ls -la ~/Downloads` **Wrong classification?** Adjust the keywords in RULES to match your files better. **Want to undo?** Add logging so you can reverse moves if needed. ## Next Steps - [Building custom skills](/post/building-custom-skills-a-developers-guide-to-openclaw-extensions) - [Scheduling with OpenClaw cron](/post/google-tasks-integration-syncing-your-to-do-list-with-ai-agents) - [Web scraping for data collection](/post/web-scraping-101-using-openclaw-browsing-tools-for-data-collection)