Nodes/ComfyUI-TinyBee/Sanitize File Path
ComfyUI Node

Sanitize File Path

Make a string safe to use as a filename

By TinyBeeman·Created about a year ago·Updated about a month ago· 1
Sanitize File Path
    • sanitized_path
    path
    replace_spaces
    replace_invalid_chars_
    extra_invalid_chars

    Sanitize File Path scrubs a string clean of the characters that break file names, so you can use a prompt or arbitrary text as a filename without ComfyUI (or your OS) throwing a fit. Feed it "a portrait, 4k" and get back "a_portrait_4k" - safe to save, safe to sort, safe to send anywhere.

    Why you'd reach for it

    The moment you let user text become a filename - saving images named after their prompts, building file paths from metadata, writing batch outputs - you run into the forbidden set: on Windows that's <>:"/\|?*, and colons and slashes are trouble on every platform. Hand-rolling that cleanup in a Search and Replace node means a tangle of nested replacements. This node does the whole scrub in one step and hands you a string that won't explode a save node.

    How it works

    It runs a regex over the input that replaces every character in ,<>:"/\|?* with replace_invalid_chars (default _). Then two optional passes:

    • if replace_spaces is set to something other than a single space (its default), spaces get swapped for that character - set it to _ for no_spaces filenames;
    • if extra_invalid_chars is filled in, those characters are also scrubbed (the string is escaped first, so regex metacharacters like [ or ] are handled safely).

    Note the defaults are conservative: by default it only fixes the truly illegal characters and keeps spaces. The output is a plain STRING you can feed into any Save node's filename field.

    The inputs that matter

    • path - the text to sanitize.
    • replace_spaces - default " " (keep spaces). Change to _ to remove them.
    • replace_invalid_chars - what illegal chars become, default _.
    • extra_invalid_chars - additional characters to strip, in addition to the built-in set.

    One output:

    • sanitized_path - the cleaned string.

    Installing it

    TinyBee pack: ComfyUI Manager → Install Custom Nodes → search "ComfyUI-TinyBee", install, restart. Manual:

    cd ComfyUI/custom_nodes
    git clone https://github.com/TinyBeeman/ComfyUI-TinyBee
    

    Under 🐝TinyBee/Strings. Stdlib only (re is built in).

    Common issues

    The big expectation gap is the slashes. The built-in set always removes / and \, so you can't use this node to preserve directory structure - "folder/image" becomes "folder_image", not a subpath. If you need real subfolders, sanitize the components separately and join them yourself. Second gotcha: spaces survive by default, so if you're aiming for underscore-style names you must set replace_spaces to _ explicitly. And if a filename still fails to save after sanitizing, look at reserved names and length limits (like Windows' CON or the 255-char cap) - those are OS rules this node can't see.

    Category🐝TinyBee/Strings

    Inputs (4)

    NameTypeDefaultDescription
    pathSTRING
    replace_spacesSTRING
    replace_invalid_charsSTRING_
    extra_invalid_charsSTRING

    Outputs (1)

    NameTypeDescription
    sanitized_pathSTRING