name: document-batch-pipeline
description: Converts, extracts, renames, and archives a folder of documents in a repeatable batch job with a dry run, a manifest, and per-file error isolation. Use when processing dozens or thousands of PDFs, images, invoices, or scans, or when a one-off conversion script needs to become rerunnable.
license: MIT
metadata:
author: community-list
version: "1.0.0"
created: "2026-11-22"
Document Batch Pipeline
A batch job that can be run twice safely, reports exactly what it did, and does not lose the
originals.
When to use
- Converting a folder of documents between formats.
- Extracting text, tables, or metadata from many files.
- Renaming or filing documents by content.
- Re-running a conversion after fixing one bad file.
Workflow
1. Inventory before touching anything
Produce a manifest of inputs: path, size, type (by content, not extension), page count where
relevant, and a SHA-256 hash. Report the count and total size. Stop if the count is zero or if
the folder contains unexpected file types beyond a stated threshold.
2. Define the transformation precisely
Write the contract before code:
| Aspect |
Decision |
| Input types accepted |
e.g. PDF 1.4+, JPEG, PNG |
| Output naming |
e.g. <source-stem>.txt, collisions get -2 |
| Output location |
A new directory, never the input directory |
| Encoding |
UTF-8, LF line endings |
| Metadata to preserve |
Original path, hash, source mtime |
| Failure policy |
Isolate, log, continue; never abort the whole batch |
3. Dry run first
--dry-run prints the planned operation per file and writes plan.json. No writes occur.
Show the first 20 lines and the total counts. Require confirmation for destructive operations
(overwrite, delete, move).
4. Execute with isolation
- Process one file at a time in a
try/catch; one failure must not stop the batch.
- Write outputs atomically: write to a temp file, then rename.
- Record per-file status in
manifest.json: ok, skipped, failed with a reason.
- Log progress every N files with counts, not per-file spam.
- Enforce a timeout per file; a hung OCR call should not stall the job.
5. Verify
- Output count equals successful input count.
- Spot-check 3 files by opening the output, not just by checking size.
- Re-run on the same input: results must be identical (idempotent), and the manifest should
report
skipped: already processed using hashes.
- Check for zero-byte outputs and flag them as failures, not successes.
6. Summarise
Processed 1,284 files in 6m 12s
ok 1,271
skipped 9 (unchanged hash)
failed 4
Failures:
invoices/scan_0442.pdf – encrypted, password required
...
7. Make it rerunnable
- Keep the manifest; it is the resume state.
--resume skips files already marked ok with a matching hash.
- Version the script; note the version in the manifest.
Rules
- Never modify or delete inputs. Outputs go to a separate directory.
- Never process files outside the stated input directory, including via symlinks that escape it.
- Never send document contents to a third-party service without explicit permission.
- Never report a file as converted without verifying the output is readable.
- Do not silently drop unreadable files; they must appear in the failure list.
Output format
out/
├── <converted files>
├── manifest.json
└── summary.txt
manifest.json per entry: { input, sha256, bytes, status, output, error, duration_ms }.
Error handling
| Condition |
Action |
| Encrypted or password-protected file |
Mark failed: encrypted, continue |
| Corrupt file |
Mark failed: unreadable, continue, keep the original |
| Disk full |
Abort cleanly, keep the manifest, report the last completed file |
| Filename collision |
Append -2, -3; record the mapping |
| Very large batch (>50k) |
Chunk by directory, report progress per chunk |
Example
Input: 1,284 scanned invoices, target: searchable PDF plus extracted text.
Output: out/, manifest.json with 1,271 ok / 9 skipped / 4 failed, summary.txt, and a
--resume run that completes in 40 seconds by skipping unchanged inputs.
1---2name: document-batch-pipeline3description: Converts, extracts, renames, and archives a folder of documents in a repeatable batch job with a dry run, a manifest, and per-file error isolation. Use when pro4---5---6name: document-batch-pipeline7description: Converts, extracts, renames, and archives a folder of documents in a repeatable batch job with a dry run, a manifest, and per-file error isolation. Use when processing dozens or thousands of PDFs, images, invoices, or scans, or when a one-off conversion script needs to become rerunnable.8license: MIT9metadata:10 author: community-list11 version: "1.0.0"12 created: "2026-11-22"13---1415# Document Batch Pipeline1617A batch job that can be run twice safely, reports exactly what it did, and does not lose the18originals.1920## When to use2122- Converting a folder of documents between formats.23- Extracting text, tables, or metadata from many files.24- Renaming or filing documents by content.25- Re-running a conversion after fixing one bad file.2627## Workflow2829### 1. Inventory before touching anything3031Produce a manifest of inputs: path, size, type (by content, not extension), page count where32relevant, and a SHA-256 hash. Report the count and total size. Stop if the count is zero or if33the folder contains unexpected file types beyond a stated threshold.3435### 2. Define the transformation precisely3637Write the contract before code:3839| Aspect | Decision |40|--------|----------|41| Input types accepted | e.g. PDF 1.4+, JPEG, PNG |42| Output naming | e.g. `<source-stem>.txt`, collisions get `-2` |43| Output location | A new directory, never the input directory |44| Encoding | UTF-8, LF line endings |45| Metadata to preserve | Original path, hash, source mtime |46| Failure policy | Isolate, log, continue; never abort the whole batch |4748### 3. Dry run first4950`--dry-run` prints the planned operation per file and writes `plan.json`. No writes occur.51Show the first 20 lines and the total counts. Require confirmation for destructive operations52(overwrite, delete, move).5354### 4. Execute with isolation5556- Process one file at a time in a `try`/`catch`; one failure must not stop the batch.57- Write outputs atomically: write to a temp file, then rename.58- Record per-file status in `manifest.json`: `ok`, `skipped`, `failed` with a reason.59- Log progress every N files with counts, not per-file spam.60- Enforce a timeout per file; a hung OCR call should not stall the job.6162### 5. Verify6364- Output count equals successful input count.65- Spot-check 3 files by opening the output, not just by checking size.66- Re-run on the same input: results must be identical (idempotent), and the manifest should67 report `skipped: already processed` using hashes.68- Check for zero-byte outputs and flag them as failures, not successes.6970### 6. Summarise7172```73Processed 1,284 files in 6m 12s74 ok 1,27175 skipped 9 (unchanged hash)76 failed 477Failures:78 invoices/scan_0442.pdf – encrypted, password required79 ...80```8182### 7. Make it rerunnable8384- Keep the manifest; it is the resume state.85- `--resume` skips files already marked `ok` with a matching hash.86- Version the script; note the version in the manifest.8788## Rules8990- Never modify or delete inputs. Outputs go to a separate directory.91- Never process files outside the stated input directory, including via symlinks that escape it.92- Never send document contents to a third-party service without explicit permission.93- Never report a file as converted without verifying the output is readable.94- Do not silently drop unreadable files; they must appear in the failure list.9596## Output format9798```99out/100├── <converted files>101├── manifest.json102└── summary.txt103```104105`manifest.json` per entry: `{ input, sha256, bytes, status, output, error, duration_ms }`.106107## Error handling108109| Condition | Action |110|-----------|--------|111| Encrypted or password-protected file | Mark `failed: encrypted`, continue |112| Corrupt file | Mark `failed: unreadable`, continue, keep the original |113| Disk full | Abort cleanly, keep the manifest, report the last completed file |114| Filename collision | Append `-2`, `-3`; record the mapping |115| Very large batch (>50k) | Chunk by directory, report progress per chunk |116117## Example118119**Input:** 1,284 scanned invoices, target: searchable PDF plus extracted text.120**Output:** `out/`, `manifest.json` with 1,271 ok / 9 skipped / 4 failed, `summary.txt`, and a121`--resume` run that completes in 40 seconds by skipping unchanged inputs.