Nodes/ComfyUI-QING/目录随机丨文件
ComfyUI Node

目录随机丨文件

Pick a random file from a folder — reference, LoRA, or prompt — as a path

By sheengoa·Created about a year ago·Updated 19 days ago· 16
目录随机丨文件
    • file_path
    path
    seed-1
    exclude_hiddentrue

    Sometimes you don't want to pick the reference image. You want the graph to pick a random one from a folder and shuffle the results every run - like an A1111 wildcard, but for files on disk instead of words. RandomDirectoryItem is the QING pack's version: give it a directory, get back the full path to one random file inside it. Drop the path into a LoadImage, a prompt-text loader, or a LoRA path and you've got instant batch variety.

    What it does

    Inputs: path (the directory to search), seed (default -1 = random every run; a fixed non-negative value makes the pick reproducible), and exclude_hidden (default true - skip dotfiles and hidden attributes on both files and directories, Windows included, not just .-prefixed names).

    Output: file_path (STRING), the full normalized path to the chosen file.

    The search is recursive - os.walk through the whole tree, collecting every file, then random.Random(seed).choice(). The file list is sorted first, which matters: it makes a fixed seed stable across runs (same order, same choice) regardless of filesystem enumeration order. Two edge cases handled gracefully: if path is actually a single file, it just returns that file's path; if the folder doesn't exist or is empty, it returns an error string instead of crashing.

    Where you'd reach for it

    Reference shuffling is the main event: a folder of reference images, a random one each run, fed to an IPAdapter or image-to-video start frame. Same trick works for prompt files (random prompt each run from a folder of .txt), style transfers, and LoRA directories - RandomDirectoryItem outputs a path, and most loaders take a path. Because it's an output node (OUTPUT_NODE = True), it always executes, so the -1 seed actually rolls fresh on every run - the reliable version of the always-rerun pattern from the KB's plumbing doc.

    Installing it

    cd ComfyUI/custom_nodes
    git clone https://github.com/GAO-SHIQING/ComfyUI-QING
    cd ComfyUI-QING
    python install_dependencies.py
    

    Restart ComfyUI after. Pure stdlib (os, random), zero dependencies. Manager: search "ComfyUI-QING." (README's clone URL prints GAOSHI-QING - typo; the repo is GAO-SHIQING/ComfyUI-QING.)

    Things to know

    The error-handling design is the one thing to remember: a bad path or empty folder doesn't raise, it returns an error string like ❌ 路径不存在或不是目录: /nope. That string then flows downstream as a "valid" path - so if your loader starts choking on paths with ❌ in them, check this node first. With exclude_hidden on, a folder that's all hidden files reports as empty even though files exist. And if you're using a fixed seed for reproducibility, keep the file list stable - adding or removing a file shifts the sorted order and changes which file the seed maps to. That's not a bug, it's just what "choice" means.

    CategoryQING/数据工具

    Inputs (3)

    NameTypeDefaultDescription
    pathSTRING
    seedINT-1-1–2147483647
    exclude_hiddenBOOLEANtrue

    Outputs (1)

    NameTypeDescription
    file_pathSTRING