Nodes/ComfyUI-FilePathsFromFolder/Get File Path By Index
ComfyUI Node

Get File Path By Index

Pluck one file out of the list by number

By RockyHong·Created 7 months ago·Updated 7 months ago· 1
Get File Path By Index
    • filepath
    • filename
    filepaths
    filenames
    index0

    LoadFilePathsFromFolder hands you a whole folder as one newline-joined string and a count. This node is the other half of that handshake: you give it the string plus an index, and it hands back the single file that index points to. It's the "pick an item" node in the ComfyUI-FilePathsFromFolder pack, and its whole reason to exist is external loop packs.

    If you already run a for/while loop from Impact Pack, rgthree, or one of the other loop suites, this is how you bridge "the loop knows it's on iteration 3" and "the loop actually loads file number 3." The pattern from the pack README: loop indexGetPathByIndex.index, LoadFilePathsFromFolder.filepaths and .filenamesGetPathByIndex, then GetPathByIndex.filepath → your loader. Boring, mechanical, exactly what you want from plumbing.

    The inputs

    • filepaths - the newline-joined path list straight from LoadFilePathsFromFolder. It's a multiline string, so yes, you can paste one manually too, but wiring it is the point.
    • filenames - the matching name list, same order. Wire it from the same source node so the two lists stay aligned.
    • index - which entry to pull, an INT with a minimum of 0. Wire this from your loop's current iteration counter and it updates every iteration.

    The outputs

    • filepath - the full path of the chosen file (e.g. C:\data\inputs\cat_003.png). Feed this into Load Image, Load Video, Load Audio, or any node that takes a path.
    • filename - the base name without the extension (cat_003). Useful for naming your output, or for pairing with a same-basename .txt caption file in a batch.

    Both come out as plain STRINGs, one item each. That's the loop-iteration contract: one loop step, one file.

    How it works, and the one gotcha

    Mechanically it's a split("\n") on each input string, then an index into the resulting list. That's it - there's no magic, no model, no disk access. The gotcha lives in the edge case: if your index is greater than or equal to the number of files, the node returns an empty string rather than erroring. So a loop that overruns the count will silently feed your loader an empty path and probably fail downstream in a confusing way. Realistically your loop should be driven by LoadFilePathsFromFolder.count so this never happens - but if you're stepping manually and wonder why "no image loaded," check that the index isn't past the end of the list.

    Also worth knowing: the file list is sorted alphabetically by the parent node before it ever reaches you, so "index 0" is the first file alphabetically, not the first one in the folder's creation order. That's deterministic and repeatable, which is what you want for batch runs, just don't expect file-system order.

    Install and wrap-up

    Part of the same pack, so one install covers it: git clone https://github.com/RockyHong/ComfyUI-FilePathsFromFolder.git into ComfyUI/custom_nodes, restart, then find "Get File Path By Index" in the node menu. No dependencies, no downloads - the whole pack is stdlib Python. If you don't have an external loop pack yet and just want "once per file," you might skip this node entirely and use the pack's own File Paths For Loop Start instead, which combines the index picking and the loop into one flow. Reach for GetPathByIndex when you already have a loop pack you like and don't want to learn another one.

    CategoryFilePathsFromFolder

    Inputs (3)

    NameTypeDefaultDescription
    filepathsSTRING
    filenamesSTRING
    indexINT0

    Outputs (2)

    NameTypeDescription
    filepathSTRING
    filenameSTRING