GC-301g · Module 2
Multi-File Operations
3 min read
Multi-file operations apply the same Gemini prompt to every file matching a pattern — generating docs for each module, reviewing each component, migrating each config file. The fundamental pattern is a loop: enumerate targets, process each one, collect results. The key design decision is granularity: process one file per invocation (maximum context per file, slower) or batch multiple files per invocation (faster, but context competition between files). For code review and documentation, one file per call produces better results. For pattern searches and audits, batching is acceptable.
Result collection requires a consistent naming convention. Map input filenames to output filenames deterministically — src/api/auth.ts produces docs/api/auth.md, reviews/api/auth.json, or tests/api/auth.test.ts. This mapping makes it trivial to correlate inputs with outputs, rerun failed items, and merge results into a single report. Never use timestamps or random names for batch outputs — you lose the ability to identify which input produced which result.
#!/bin/bash
# Multi-file documentation generator
SRC_DIR="src/api"
OUT_DIR="docs/generated/api"
mkdir -p "$OUT_DIR"
successful=0
failed=0
for file in "$SRC_DIR"/*.ts; do
[ -f "$file" ] || continue
name=$(basename "$file" .ts)
echo "[$((successful + failed + 1))] Processing $name..."
if gemini -p "Generate comprehensive API documentation for this TypeScript module. Include function signatures, parameter descriptions, return types, and usage examples.\n\n$(cat "$file")" \
> "$OUT_DIR/$name.md" 2>/dev/null; then
((successful++))
else
echo " FAILED: $name" >&2
((failed++))
fi
done
echo "\nBatch complete: $successful succeeded, $failed failed"