Nodes/comfyui-oli-prompt-tools/Sanitize Filename (Oli)
ComfyUI Node

Sanitize Filename (Oli)

Make your file names safe before save nodes choke on them

By magicoli·Created 6 months ago·Updated 5 months ago· 0
Sanitize Filename (Oli)
    • prefix
    • path
    • filename
    filename
    folder
    extension
    allow_spacestrue
    allow_slashtrue
    allow_unicodefalse
    max_length240
    replacement_

    Sanitize Filename (Oli) takes a string - a prompt, a title, a generated tag - and turns it into something Windows, macOS, and your save node will all accept. It's the boring-but-necessary node you reach for the first time a generated file fails to write because the filename contained a /, a :, or a control character that made the OS (or your importer) throw up.

    How it works

    It's a whitelist filter, not a "remove the bad characters I know about" filter. Anything that isn't explicitly allowed gets replaced:

    • ASCII letters and digits always survive.
    • Hyphens survive.
    • Spaces survive if allow_spaces is on (default).
    • Forward slashes survive if allow_slash is on (default) - treated as path separators, so a "folder/file" input builds a real subpath.
    • Unicode survives only if allow_unicode is on. Off (the default), diacritics are transliterated via NFKD: à→a, é→e, ü→u, ñ→n. Characters with no ASCII decomposition get dropped.

    Everything else becomes replacement (default _; must be a single non-alphanumeric ASCII character - invalid values fall back to _). Runs of two or more separators collapse to the highest-priority one (/ > - > space > replacement), so a messy name doesn't become a run of underscores. It also normalizes backslashes to forward slashes, trims junk from the edges, and truncates to a byte limit.

    Inputs that matter

    • filename - the name to sanitize (may include / separators).
    • folder - an optional prefix path; slashes are always allowed here.
    • extension - with or without the leading dot; it's stripped and re-added cleanly.
    • max_length - caps the filename component at N UTF-8 bytes (default 240). The default deliberately leaves room for the counter and extension that save nodes append - the OS limit is 255 bytes. Note it's bytes, not characters, so accented letters (2 bytes each) count double.
    • replacement - the substitution character.

    Outputs

    Three strings, so you can feed whichever a save node wants: prefix (folder/filename without the extension - the thing Save Image-style nodes take), path (folder/filename.ext), and filename (just filename.ext, no folder).

    Install & gotchas

    cd ComfyUI/custom_nodes
    git clone https://github.com/magicoli/comfyui-oli-prompt-tools
    

    …or ComfyUI Manager → Oli Prompt Tools, then restart. No dependencies.

    It's one of the pack's newer nodes, so it's missing from the README's node table - it loads fine, the docs just lag the code. And a friendly warning: because allow_unicode defaults to off, a filename full of non-Latin script loses most of its characters. If your prompts are in Japanese or Arabic, turn allow_unicode on or you'll end up with a pile of underscores.

    CategoryOli/utils

    Inputs (8)

    NameTypeDefaultDescription
    filenameoptSTRINGThe filename to sanitize.
    folderoptSTRINGThe folder path to sanitize.
    extensionoptSTRINGFile extension (with or without leading dot).
    allow_spacesoptBOOLEANtrue
    allow_slashoptBOOLEANtruePreserve / as path separator in the filename input.
    allow_unicodeoptBOOLEANfalseIf disabled, diacritics are transliterated via NFKD: à→a, é→e, ü→u, etc.
    max_lengthoptINT2400–4096Max bytes for the filename component (0 = no limit). Default 240 leaves room for the counter and extension that save nodes typically append. OS limit is 255 bytes.
    replacementoptSTRING_Character used in place of disallowed characters. Must be a single non-alphanumeric ASCII character; invalid values fall back to _.

    Outputs (3)

    NameTypeDescription
    prefixSTRING
    pathSTRING
    filenameSTRING