Nodes/ComfyUI-JNodes/Select Random File From Directory
ComfyUI Node

Select Random File From Directory

Pull a random file path from a folder each run

By JaredTherriault·Created 3 years ago·Updated about a month ago· 91
Select Random File From Directory
    • PATH
    base_directory
    include_subdirectoriestrue
    optional_file_type
    seed0

    Point this at a folder and it hands back the path to a random file inside it. That's the pitch, and it's more useful than it sounds. Doing a batch where you want a different source image every run? Random driving video for img2video? Roulette over a folder of references? This is the node that picks for you, so you're not manually swapping a LoadImage every generation.

    It fits the JNodes philosophy - Jared Therriault's suite is full of small "stop doing this by hand" helpers - and it pairs naturally with the pack's own media loaders. The readme spells out the intended flow: it returns a path, which you then feed into one of the media nodes to actually load the file.

    How it works

    You give it a base directory and a seed. It scans that folder (and optionally its subfolders), optionally filters by file type, and returns one random file's path as a STRING. The randomness is seeded, so the same seed always picks the same file - which is the key behavior to understand. To actually get variety across a batch, you change the seed each run (wire in a random-seed node, or set it to increment), exactly like you'd randomize a sampler seed.

    The inputs and outputs

    • base_directory (STRING) - the folder to pick from. Wire in JNodes_GetOutputDirectory or type a path.
    • seed (INT) - controls which file gets chosen. Same seed → same file; change it to vary.
    • include_subdirectories (default true) - whether to recurse into child folders.
    • optional_file_type (STRING) - filter to an extension (e.g. limit to images, or to a video type) so you don't accidentally grab a .txt.
    • PATH output (STRING) - the chosen file's path. This does not load the file; it's a path string.

    How to install it

    Via ComfyUI Manager: Install Custom Nodes, search "JNodes", install, restart. Or by hand:

    cd ComfyUI/custom_nodes
    git clone https://github.com/JaredTherriault/ComfyUI-JNodes
    pip install -r ComfyUI-JNodes/requirements.txt
    

    then restart ComfyUI. No model downloads.

    Common issues & troubleshooting

    It returns a path, not an image. This is the most common confusion. The output is a STRING pointing at a file - you still need a loader downstream. Plug the PATH into JNodes_LoadVisualMediaFromPath (for images or video) to actually bring the pixels into the graph.

    Same file every time? Change the seed. Because selection is seeded, a fixed seed deterministically returns the same file. That's a feature for reproducibility, but if you wanted variety and forgot to randomize the seed, you'll get the identical file on every run. Wire in a randomizing seed, or set the seed widget to increment.

    Wrong file types sneaking in - use optional_file_type. If a folder mixes images, videos, and stray text/JSON files, an unfiltered pick can grab something your loader chokes on. Set optional_file_type to constrain the pool to what you can actually consume.

    The directory has to be readable by the server. On a local install this is just a normal folder path. On a hosted or serverless ComfyUI, a path from your own machine won't exist over there - the folder has to be present in the execution environment. If you get empty or missing-file errors, that mismatch is the usual cause.

    Empty folder or over-tight filter → nothing to pick. If the directory is empty, or your optional_file_type filter matches zero files, there's nothing to return. Loosen the filter or check the path.

    Categorysd

    Inputs (4)

    NameTypeDefaultDescription
    base_directorySTRING
    include_subdirectoriesBOOLEANtrue
    optional_file_typeSTRING
    seedINT0

    Outputs (1)

    NameTypeDescription
    PATHSTRING