Nodes/ComfyUI-LZNodes/LZ String Sanitize
ComfyUI Node

LZ String Sanitize

Make a prompt safe to use as a filename

By liz-ils·Created 7 months ago·Updated 12 days ago· 1
LZ String Sanitize
    • string
    text
    mode
    replace_with_

    You can't name a file 1girl, school uniform, (smile:1.3)!.png on most operating systems - the :, the /, the " all get you errors or silent mangling. LZ String Sanitize exists for exactly this: feed it any text and it strips (or replaces) the characters that are illegal in filenames, so you can use a prompt, a tag, or a label as a filename prefix safely.

    It's a small node and proud of it. Two modes, one optional extra:

    • text - the string to clean.
    • mode - remove (delete the bad characters) or replace (swap them for something else, default _).
    • replace_with - used in replace mode; defaults to _.
    • Output: string - the cleaned text.

    What it actually kills

    The banned set, straight from the source: backslash, forward slash, question mark, double quote, <, >, colon, pipe, and asterisk - the classic \/?"<>\:|* Windows-filename blacklist. It also strips a trailing period, because Windows reserves that too. The same set applies on Linux and macOS for practical sanity, so this isn't a Windows-only nicety; it's just the characters no OS should ever see in a path component.

    In replace mode, a prompt like 1girl, (smile:1.3) becomes 1girl, _smile_1.3_ (colons, parens, and spaces all handled - the banned set gets _, the rest is untouched). In remove mode the bad characters vanish entirely, which tends to glue words together and is mostly useful when you only care that the filename is legal, not that it reads nicely.

    Where you'd use it

    The standard pattern is a filename assembly chain: build a label from your prompt or a tag selector, run it through sanitize, then hand the result to a save node's filename_prefix. The pack's LZSaveImageAndLog takes a prefix string, so sanitize sits right before it. You can also wire it into LZSaveStringToCSV's filepath if you're generating CSV names from content.

    The honest limitation: it only fixes the filename characters. It won't cap the length, and a very long prompt makes a very long (and legally weird) filename - fine for most filesystems, but annoying if you're on one with a 255-character per-component limit. If you're feeding an entire paragraph-length prompt in, truncate it first or use it as a prefix rather than the whole name.

    Installing it

    Same pack as everything else here:

    cd ComfyUI/custom_nodes
    git clone https://github.com/liz-ils/ComfyUI-LZNodes
    

    Restart ComfyUI, or grab it from ComfyUI Manager by searching "ComfyUI-LZNodes". No extra dependencies, no models, no config.

    The sibling worth mentioning: LZStringSelect, which picks one string out of ten by index - combine the two and you can build a rotating set of filename-safe labels across a batch run.

    CategoryMyCustomNodes/Text

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING
    modeCOMBO2 options: remove, replace
    replace_withoptSTRING_

    Outputs (1)

    NameTypeDescription
    stringSTRING