ComfyUI Node

Path Sanitizer

Path Sanitizer Fixes the Filename First

By logicalor·Created 8 months ago·Updated 8 months ago· 0
Path Sanitizer
    • sanitized_path
    input_string
    replacement_char_
    max_length255
    lowercasefalse
    strip_spacesfalse
    collapse_replacementtrue

    You know the failure: you build a workflow that names its output from the prompt, run it with a great idea, and the save node quietly refuses or mangles the file. The culprit is usually a : from a timestamp, a / that snuck into a filename from a prompt phrase, or a Windows-reserved name like CON. Path Sanitizer is a one-node pack that fixes the string before it ever reaches the save node, so your output names stay readable and your workflow stops dying on punctuation.

    It's not a glamorous node. It's plumbing - the kind of thing that earns zero community drama because when it works, you never think about it again. But if you've ever handed a save node a prompt:style/seed string and watched it choke, this is the fix.

    What it actually does

    The node lives under utils/string and takes one string in, returns one string out. Under the hood it's pure Python stdlib - re and unicodedata, nothing to install - running this pipeline:

    • NFKC unicode normalization, so full-width and compatibility characters collapse to their standard form before anything else happens.
    • Invalid character replacement. Anything matching [<>:"/\\|?*\x00-\x1f] gets swapped for your replacement_char (default _). Note the slash and backslash are in there, which matters - more on that below.
    • Windows reserved names. If the base name is CON, PRN, AUX, NUL, or COM19 / LPT19, it gets prefixed (so CON becomes _CON). Those names are illegal on Windows no matter where the file lives, so the node treats them as universal hazards.
    • Trailing period/space stripping - Windows forbids those at the end of filenames.
    • Length truncation that tries to keep the extension intact, so a_really_long_name...png stays a .png after it's cut down to your max_length.

    If everything gets stripped and the result is empty, it returns "unnamed" rather than an empty filename. Small touch, saves a crash.

    The inputs that matter

    Only one is required; the rest have sane defaults and you'll rarely touch most of them.

    • input_string (required) - the string to clean. Wire this from your prompt-builder, timestamp node, or whatever's feeding the filename.
    • replacement_char (default "_") - what invalid characters become. If you'd rather see hyphens than underscores, set it to -.
    • collapse_replacement (default true) - squashes runs of the replacement char into one, so My///Path becomes My_Path instead of My___Path. Leave it on.
    • max_length (default 255) - caps output length and preserves the extension when truncating.

    The output is a single sanitized_path string. It wires straight into the filename input of whatever save node you're using.

    Installing it

    Via ComfyUI Manager, search "Path Sanitizer" and hit Install. Or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/logicalor/comfyui_path_sanitizer.git
    

    Then restart ComfyUI. There are no model downloads and no heavy dependencies - the whole pack is one Python file and a pyproject. It's about as frictionless as a custom node gets.

    Where people get burned

    The big one: it sanitizes a path component, not a full path. Because / and \ are in the invalid-character set, feeding it C:/output/images/foo will happily turn the slashes into underscores. If you want a multi-folder path, run one node per segment and join them, or sanitize just the filename and keep the directory static.

    A couple more gotchas worth knowing:

    • max_length is capped at 255 - matching Windows's per-component limit. You can't ask for a 500-char filename, which is honestly correct.
    • The replacement char is a single character. If you type --, the node keeps only the first char (and falls back to _ if even that is invalid). Feed it one char.
    • It doesn't lowercase by default, and it preserves spaces unless you flip strip_spaces on. Both are toggles - handy when you're generating files to share across OSes and want to force everything to lowercase-with-no-spaces.

    Is this the node you'd reach for over, say, the string utilities bundled in WAS Node Suite or Efficiency Nodes? If you already have a big utility pack installed, it probably does something similar. But if you want exactly one thing, done with no dependencies and no config, this is a clean grab. It doesn't call any API, needs no key, and can't really break your setup - the worst case is you don't need it.

    Categoryutils/string

    Inputs (6)

    NameTypeDefaultDescription
    input_stringSTRINGThe string to sanitize for filesystem use
    replacement_charoptSTRING_Character to replace invalid characters with
    max_lengthoptINT2551–255Maximum length of the sanitized path component
    lowercaseoptBOOLEANfalseConvert the result to lowercase
    strip_spacesoptBOOLEANfalseRemove all spaces from the result
    collapse_replacementoptBOOLEANtrueCollapse consecutive replacement characters into one

    Outputs (1)

    NameTypeDescription
    sanitized_pathSTRING