One Big Mistake in Batch Renamers
The biggest trust breaker in file renaming tools is simple: users click apply and discover they cannot recover a bad pattern. Dry-run and rollback are not “nice to have.” They are core safety features.
Step 1: Compile rename plan before writing anything
from dataclasses import dataclass
@dataclass
class RenameOp:
src: str
dst: str
def plan_ops(files, transform):
return [RenameOp(src=f, dst=transform(f)) for f in files]
Step 2: Validate collisions and invalid targets
def validate_ops(ops):
targets = [op.dst for op in ops]
if len(targets) != len(set(targets)):
raise ValueError("target collision detected")
if any(not t.strip() for t in targets):
raise ValueError("empty filename target")
Step 3: Write rollback journal on execute
[
{"src": "raw-001.png", "dst": "cover-001.png"},
{"src": "raw-002.png", "dst": "cover-002.png"}
]
Pitfalls
- Executing rename operations without preview mode.
- No rollback artifact for partial failures.
- Path normalization not handled across platforms.
Verification
- Dry-run preview exactly matches executed operations.
- Rollback restores original names after simulated crash.
- Collision checks block unsafe patterns.