Batch Edit Text Files ๐
Bulk-edit every caption in a folder at once
- trigger_signal
- updated_file_count
- log
BatchEditTextFiles is the caption workhorse of the MieNodes pack. It opens every text file in a folder and performs one edit across all of them - insert something at the front, append to the end, replace one string with another, or remove a string entirely. If you train LoRAs, this is the node that turns "manually edit 60 .txt files" into one graph run.
It comes from ComfyUI-MieNodes by MieMieeeee, and it sits at the center of the pack's whole reason to exist: making LoRA caption prep bearable inside ComfyUI.
Why this matters for training
Captions are one of the highest-leverage things in a training run - the KB is emphatic that captioning is among the most impactful factors, and that the durable rule is describe what you want to stay adjustable, leave what should be permanent undescribed. In practice that means a lot of bulk surgery on a folder of .txt files:
- Add a trigger word to every caption. Rare tokens like
ch9racternameare still the standard way to give a concept a clean handle.insertputs it at the front of all your captions in one pass. - Fix a tag you got wrong across the whole set.
replaceswapsblue eyesforgreen eyeseverywhere, instead of you opening 60 files. - Strip a tag that's baking in something you want variable.
removedeletes it from every file.
Doing this by hand is exactly the kind of tedium that makes people cut corners on captioning - and cut corners on captioning is how you get overfit, uncontrollable LoRAs.
How it works
Point directory at the caption folder and pick an operation: insert, append, replace, or remove. Then fill the text inputs that the operation needs:
new_text- the content to insert, append, or use as the replacement.target_text- the string to find, used byreplaceandremove.file_extension(default.txt) - which files to touch. Captions are almost always.txt, so you'll rarely change this.
insert and append only need new_text. replace needs both - it finds target_text and swaps in new_text. remove needs target_text and deletes it. There's also an optional trigger_signal (*) that's purely for forcing execution order relative to other file nodes; it carries no data.
Outputs are updated_file_count (INT) and log (STRING). Wire the count into a ShowAnything node so you can confirm it actually hit every file and not, say, 12 of 60 because half your captions were .caption not .txt.
Installing it
Install the whole pack. ComfyUI Manager โ search ComfyUI-MieNodes (ComfyUI_MieNodes) โ install โ restart. Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/MieMieeeee/ComfyUI-MieNodes
then restart. No model download; the nodes appear under the sheep-emoji ๐ MieNodes menu.
Common issues
It edits files in place. There's no backup copy. Before you run a replace or remove on a dataset you care about, it's worth copying the folder first - a replace with an over-broad target_text can quietly mangle every caption, and there's no undo button in a node graph.
Mind the spacing and commas. These captions are comma-separated tag lists. If you insert a trigger word, include the trailing comma and space yourself (ch9ractername, ) or you'll fuse it to the first real tag. The node writes exactly what you give it.
Check the count matches your file count. A mismatch almost always means your captions aren't the extension you set - check whether they're .txt or something else, and adjust file_extension. To wipe captions entirely and re-caption from scratch, reach for BatchDeleteFiles instead.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| directory | STRING | X://path/to/files | โ |
| operation | COMBO | 4 options: insert, append, replace, remove | |
| file_extensionopt | STRING | .txt | โ |
| target_textopt | STRING | โ | |
| new_textopt | STRING | โ | |
| trigger_signalopt | * | โ |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| updated_file_count | INT | โ |
| log | STRING | โ |