Nodes/barakapa-nodes/Directory Image Counter (Custom)
ComfyUI Node

Directory Image Counter (Custom)

Count the PNGs in a folder and bake the number into a filename

By barakapa·Created about a year ago·Updated about a year ago· 0
Directory Image Counter (Custom)
    • int
    • string
    directory_name

    A number, on demand, from your output folder

    Give this node a subdirectory name and it counts the PNG files sitting in it, then hands you that count twice - once as an integer, once as a zero-padded string. Sounds absurdly simple. It is. But it's also quietly useful once you see the pattern it enables.

    The headline use is file sequencing. ComfyUI saves to ComfyUI/output/, and if you're archiving runs by folder, knowing "there are already 42 images here" lets you number the next one 43 - which is exactly how the pack's own brkp_SaveImage behaves. This node just exposes the counter as a value you can read and route anywhere.

    How it works

    Mechanically it's thin: it resolves your directory_name against ComfyUI's output directory (via the same folder_paths helper ComfyUI's own save nodes use), lists the files, and counts the ones ending in .png. Two details matter:

    • The string output is zero-padded to five digits - "00042", not "42". That's deliberate: consistent-width numbers sort correctly and look tidy in filenames.
    • It only counts .png, nothing else. A folder of jpgs reads as zero.

    There's also a IS_CHANGED trick in the source worth knowing about: the node always reports itself as changed (returning NaN, which is never equal to anything), so it re-runs every execution no matter what. That's correct behavior - the folder contents change outside the graph, so a cached answer would be wrong. The cost is that this node is always "dirty" for ComfyUI's cache, which can make whatever hangs off it re-run more than you'd like. Fine for a counter; worth remembering if you chain a big expensive tree after it.

    Using it

    The one input, directory_name, is a plain string that names a subdirectory of your output folder. Leave it empty and it counts everything in output/ itself. Outputs are int and string.

    Bonus: the directory name supports the pack's search-and-replace tags - things like %date:yyyy-MM-dd% - so you can count inside a dated folder that gets created automatically. That's the same tag system the pack's save nodes use, so the pieces play together.

    Installing it

    Part of barakapa-nodes. ComfyUI Manager → search barakapa → install → restart, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/barakapa/barakapa-nodes
    

    Then restart ComfyUI. No models, no extra dependencies.

    Worth it?

    If you just want a number displayed while you work, there are fancier ways to get one. But as the input to a filename-builder - the "what comes next in this folder" question - it's the right tool, and it pairs naturally with brkp_SaveImage and brkp_SaveWorkflow from the same pack. You'll either use it constantly in organized workflows or never; both are legitimate outcomes.

    Categorycustom/Directory Image Counter

    Inputs (1)

    NameTypeDefaultDescription
    directory_nameSTRING

    Outputs (2)

    NameTypeDescription
    intINT
    stringSTRING