Nodes/MD Nodes/MD: Filename Counter
ComfyUI Node

MD: Filename Counter

The counter that never resets when you blink

By MDMAchine·Created about a year ago·Updated 3 months ago· 15
MD: Filename Counter
    • formatted_counter
    • current_value
    • info
    context_keydefault_counter
    start_value1
    increment1
    padding4
    reset_counterfalse
    prefix#
    suffix
    triggertrue

    Every workflow eventually wants numbered output - #0001, #0002, and so on. The naive way is a counter widget that resets the moment ComfyUI restarts. FilenameCounterNode is the version that survives restarts, because the count lives in a JSON file on disk rather than in the node's memory.

    That persistence is the whole trick. Each counter is keyed by a context_key - give different projects different keys and they count independently - and the current value is stored in md_filename_counters.json under ComfyUI's input directory. Close the app, come back tomorrow, and Daily_Renders picks up exactly where it left off.

    How to use it

    Four required inputs cover the basics:

    • context_key - the counter's name. default_counter is fine for a single project; use something like Project_Alpha per project to keep counts separate.
    • start_value - where counting begins (default 1).
    • increment - how much each run adds (default 1).
    • padding - zero-padding width. 4 gives 0001; 8 gives 00000001. Handy when filenames need to sort alphabetically.

    The optional inputs are where the flexibility lives. prefix and suffix wrap the number - the default prefix # turns 1 into #0001. reset_counter forces the value back to start_value when you want a fresh batch. And trigger is the quiet star: ON means "increment and save," OFF means "read-only" - return the current value without touching it. That's perfect for previewing a filename before you commit to advancing the count.

    Outputs

    Three wires: formatted_counter (STRING, the padded, prefixed text - this is what you paste into a filename template or a Save node's prefix), current_value (INT, the raw number), and info (STRING, a human-readable summary of what the node did - initialized, incremented, reset).

    A clean pattern: feed formatted_counter into the filename_prefix of a save node alongside a date, and every render gets a unique, sortable, non-overwriting filename. It pairs just as naturally with this pack's FilenameTokenReplacer - drop {counter} into a template and let this node supply the value.

    Installing it

    Part of MD Nodes (MDMAchine/ComfyUI_MD_Nodes). ComfyUI Manager → search MD_Nodes → Install, then restart. Or manually:

    cd path/to/ComfyUI/custom_nodes
    git clone https://github.com/MDMAchine/ComfyUI_MD_Nodes.git
    cd ComfyUI_MD_Nodes
    pip install -r requirements.txt
    

    Restart ComfyUI. Usual pack notes: Python 3.10+, GPL v3, pure Python.

    Common issues

    The classic confusion is expecting the counter to advance when trigger is off - it won't, by design; that's the read-only mode. And because the node always considers itself changed (it forces a re-run each queue), it won't silently skip counts in batch mode. If you reset a counter and it looks wrong, check that context_key matches the one that was counting - separate keys are separate counters, and it's easy to create a second counter under a new name instead of resetting the first.

    CategoryMD_Nodes/Utility

    Inputs (8)

    NameTypeDefaultDescription
    context_keySTRINGdefault_counterCONTEXT KEY • Purpose: Unique name to identify this counter in the JSON save file. • Usage: Use different keys for different projects to keep counts separate. ⭐ e.g., 'Project_Alpha', 'Daily_Renders'
    start_valueINT10–999999START VALUE • Purpose: Initial value if key doesn't exist or is reset.
    incrementINT11–100INCREMENT • Purpose: Amount to increase counter by on each run.
    paddingINT40–8ZERO PADDING • Purpose: Number of digits to pad (e.g. 4 padding -> 0001).
    reset_counteroptBOOLEANfalseRESET COUNTER • Purpose: If True, forces counter back to 'start_value'.
    prefixoptSTRING#PREFIX • Purpose: String added before the number (e.g., '#0001').
    suffixoptSTRINGSUFFIX • Purpose: String added after the number.
    triggeroptBOOLEANtrueTRIGGER • Purpose: Controls if the counter saves and advances. • ON (Increment): Updates value and saves to disk. • OFF (Read Only): Returns current value without changing it.

    Outputs (3)

    NameTypeDescription
    formatted_counterSTRING
    current_valueINT
    infoSTRING