逐个读取TXT文件 (Iterator)
Feed a folder of caption files into your workflow, one prompt at a time
- prompt
- 文件名
If you've ever stared at a folder full of Danbooru-style captions - img_042.png sitting next to img_042.txt - you know the annoying part isn't the images, it's feeding the matching prompts through your workflow one at a time. This node is the text half of that pairing. Point it at the folder and it hands out one .txt file's contents per queue run, walking alphabetically until it's done. Used alone it's a folder-of-prompts loader; used next to ImageFileIterator it's the caption side of a proper image-plus-prompt batch pipeline.
How it works
Same engine as every iterator in this pack: a per-node index and a cached, alphabetically-sorted file list, driven by the IS_CHANGED-returns-NaN trick so it re-executes on every queue run (that's the documented always-rerun idiom, the same one random-picker nodes use). Each run it globs *.txt, reads the current file, bumps the index, and raises an exception once the folder's exhausted - the red "all N files processed" error is the designed stop, not a crash.
Two details are worth knowing:
- It only reads
.txt. No markdown, no captions with other extensions. If your captions are.captionor.txt.extra, rename them. - It has a real encoding fallback chain:
utf-8→gbk→utf-8-sig→cp1252→latin-1. Thatgbkin second position is a tell - the author is Chinese and wrote this knowing caption files from Chinese tools are often GBK-encoded. A file that makes other nodes throw aUnicodeDecodeErrorjust gets read here. If you're in the Anglosphere it mostly doesn't matter; if you work with Chinese-language caption files, this is the rare node that just handles them.
The inputs and outputs that matter
One input: folder_path - the absolute path to the folder (default is a Windows-style C:\path\to\your\folder_A; forward slashes work elsewhere).
Outputs:
prompt(STRING) - the raw contents of the current.txtfile. Feed it into a CLIP Text Encode or any text-input node.文件名(STRING) - the base filename, extension stripped (labeled in Chinese). This is the half of the sync-check: wire it (and the image iterator's文件名) intoFilenameComparator, and any image whose caption file is missing trips the gate before you burn a run on misaligned pairs.
Installing it
It ships in ComfyUI-iterator-nodes:
cd ComfyUI/custom_nodes
git clone https://github.com/GHOSTLXH/ComfyUI-iterator-nodes
then restart ComfyUI, or use ComfyUI Manager and search the pack name. Nothing to download, no dependencies beyond what ComfyUI ships.
Gotchas
Same family rules as ImageFileIterator: set the queue to "Run (on change)" mode or it's one file per manual click; the file list is cached at first scan, so files added mid-run won't show up until the path changes or you restart; and each node instance walks its own counter. One more that surprises people: the prompt output is the file's raw contents, trailing newline and all. If you see weird whitespace in your conditioning, run it through a trim or a string-format node first - the iterator does no cleaning for you.
The pairing is the point, though. Two iterators pointing at the same folder, ImageFileIterator and this node, both spitting out matching 文件名 values, a FilenameComparator holding them honest, and you've got a hands-free folder-to-prompt pipeline in about four nodes.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| folder_path | STRING | C:\path\to\your\folder_A | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| prompt | STRING | — |
| 文件名 | STRING | — |