Automatically organize and archive files using rule-based Python scripts.
Generate a starter configuration file with example rules:
python scripts/archive_files.py --create-config config.json
This creates config.json with example rules for PDFs, images, and old files.
Edit config.json to match your archiving needs:
{
"rules": [
{
"name": "PDF Documents",
"pattern": "*.pdf",
"destination": "./archive/documents/pdf",
"use_date_folder": false,
"preserve_structure": false
},
{
"name": "Images by Date",
"pattern": "*.jpg",
"destination": "./archive/images",
"use_date_folder": true,
"preserve_structure": false
}
]
}
Rule Fields:
name: Human-readable rule namepattern: File pattern to match (.pdf, .jpg, report, or for all files)destination: Where to move/copy matched filesuse_date_folder: Create YYYY-MM-DD subfolder in destinationpreserve_structure: Keep original directory structure relative to sourceTest your rules without actually moving files:
python scripts/archive_files.py /path/to/source -c config.json --dry-run
Move files to archive:
python scripts/archive_files.py /path/to/source -c config.json
Copy files (keep originals):
python scripts/archive_files.py /path/to/source -c config.json -k
{
"rules": [
{"name": "Documents", "pattern": "*.pdf", "destination": "./docs", "use_date_folder": false, "preserve_structure": false},
{"name": "Images", "pattern": "*.jpg", "destination": "./images", "use_date_folder": false, "preserve_structure": false},
{"name": "Videos", "pattern": "*.mp4", "destination": "./videos", "use_date_folder": true, "preserve_structure": false}
]
}
{
"rules": [
{
"name": "Old Files Archive",
"pattern": "*",
"destination": "./archive",
"use_date_folder": true,
"preserve_structure": true
}
]
}
{
"rules": [
{"name": "Source Code", "pattern": "*.py", "destination": "./project/src", "use_date_folder": false, "preserve_structure": true},
{"name": "Documentation", "pattern": "*.md", "destination": "./project/docs", "use_date_folder": false, "preserve_structure": false},
{"name": "Tests", "pattern": "test_*.py", "destination": "./project/tests", "use_date_folder": false, "preserve_structure": false}
]
}
The pattern field supports:
*.pdf matches all PDF filesreport_* matches files starting with "report_"readme.txt matches exactly that file* matches all files (use with caution)Uses Python's fnmatch for wildcard matching.
python scripts/archive_files.py SOURCE [-c CONFIG] [OPTIONS]
Required:
SOURCE Source directory to scan
Options:
-c, --config PATH Configuration file path (JSON)
-d, --dry-run Simulate without moving/copying files
-k, --keep-original Copy files instead of moving them
--create-config PATH Create default configuration file
When user asks to organize files, archive old files, or clean up directories:
config.json based on their needsUser: "Help me organize my Downloads folder. I want to separate PDFs, images, and everything else."
Assistant:
config.json with rules for .pdf, .jpg/.png, and Set use_date_folder: true to organize files by archive date:
./archive/images/
├── 2024-01-15/
│ ├── photo1.jpg
│ └── photo2.jpg
└── 2024-01-16/
└── photo3.jpg
Set preserve_structure: true to maintain relative paths:
Source: /data/projects/alpha/report.pdf
Destination: ./archive/projects/alpha/report.pdf
Files match the first rule that matches their pattern. Order rules from most specific to least specific.
"Config file not found"
-c config.json points to the correct path"Source directory not found"
"/path/to/source"Files not matching expected rules
*.pdf not just .pdf)--dry-run to see which rule matches each fileScript: scripts/archive_files.py
shutil for file operationsReturn Value: JSON-like dictionary with archiving results
Safety:
-k) preserves originalsMIT License (default for user-created skills)
共 1 个版本