Nodes/ComfyUI Level Pixel/Get Filename By Index In Folder [LP]
ComfyUI Node

Get Filename By Index In Folder [LP]

Stepping through a folder one file at a time

By LevelPixel·Created 2 years ago·Updated 6 months ago· 31
Get Filename By Index In Folder [LP]
    • full_filename
    • filename
    • extension
    folder_path
    file_index0
    patterns*

    Give this node a folder and a number, and it hands you back the filename sitting at that position. It's the building block for the kind of automation the pack's README describes for its sibling "Image Loader From Path" node - "the ability to sequentially receive images into the workflow at each step of the cycle by number" - except this one is leaner: it doesn't load anything itself, it just resolves which filename is at index N, so you can wire that into whatever loader or file-processing node you actually need.

    Why filename-only, separate from actually loading the file

    Splitting "which file is next" from "load that file" is genuinely useful once you're not just loading images. Maybe you're driving a loop that processes text files, or you need the filename for something other than passing it straight into an image loader - renaming an output to match, logging which source file produced which result, whatever it is. Keeping this as a pure filename-resolver means you can plug it in front of any downstream step, not just an image loader.

    The inputs and outputs that matter

    • folder_path - the folder to look in. You can type an absolute path directly, or feed it from the pack's GetComfyUIFolderPath-LP node if you want to point at one of ComfyUI's own managed model folders instead of hardcoding a path.
    • file_index - 0-based (minimum is 0), which file you want.
    • patterns - a glob filter, default * (everything). Restrict it to something like *.png if the folder has a mix of file types and you only want one kind.
    • Outputs: full_filename (name plus extension), filename (just the stem, no extension), extension (just the suffix) - split apart so you can, for instance, reuse the stem to name a matching output file without carrying over the original extension.

    How to install it

    ComfyUI Manager: search "ComfyUI Level Pixel" or ComfyUI-LevelPixel, install, restart - auto-updates via Manager's "Update ALL." Manual:

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

    Restart ComfyUI. No models, no extra dependencies - reads the folder directly off disk.

    Common issues & troubleshooting

    The node errors because the index is out of range. This is the obvious failure mode when you're driving a loop past the end of the folder's contents - the pack's README mentions a separate "File Counter" node for exactly this situation, useful for knowing how many matching files exist before you loop past the end of them.

    Files come out in an order you didn't expect. This node doesn't let you specify a sort order - it relies on whatever order the filesystem/glob naturally returns, which is typically alphabetical but isn't guaranteed across every OS and filesystem. If sequence matters, name your files so alphabetical sort already matches the order you want - zero-padded numeric prefixes (001_, 002_, …) are the reliable way to force this.

    Your pattern isn't filtering the way you expected. patterns takes a glob, not a regex - *.png is valid, something like .*\.png$ is not and won't behave the way regex syntax would suggest. Stick to simple glob wildcards (*, ?, bracket sets).

    CategoryLevelPixel/IO

    Inputs (3)

    NameTypeDefaultDescription
    folder_pathSTRING
    file_indexINT0
    patternsSTRING*

    Outputs (3)

    NameTypeDescription
    full_filenameSTRING
    filenameSTRING
    extensionSTRING