ComfyUI-FilePathsFromFolder
A simple ComfyUI custom node pack for loading and selecting files from folders, with for-loop support for batch processing.
Nodes
LoadFilePathsFromFolder
Scans a folder for files matching a specific extension pattern.
Parameters:
folder_path: Path to the folder to scanextension: File extension pattern (e.g., ".png", ".jpg", "*.txt")
Outputs:
filepaths: Line-break separated list of full file pathsfilenames: Line-break separated list of filenames without extensionscount: Number of files matched
GetPathByIndex
Selects a specific file from the lists by index.
Inputs:
filenames: Line-break separated filenames (from LoadFilePathsFromFolder)filepaths: Line-break separated filepaths (from LoadFilePathsFromFolder)
Parameters:
index: Index of the file to select (0-based)
Outputs:
filename: Selected filenamefilepath: Selected filepath
Loop Nodes
FilePathsForLoop
Initiates iteration through files in a folder. Uses ComfyUI's list-based batching to process each file individually.
Parameters:
folder_path: Path to the folder to scanextension: File extension pattern (e.g., ".png", ".jpg", "*.txt")
Outputs:
filepath: Full path of current file (list output - each item processed separately)filename: Filename without extension (list output)current_index: Current iteration index, 0-based (list output)total_count: Total number of files (list output)
Important: This node outputs lists with OUTPUT_IS_LIST = True. ComfyUI will automatically run all connected nodes once for each item in the list. Each connected node receives a single value from the list per execution, not the entire list.
FilePathsLoopClose
Closes the file paths loop and collects iteration results.
Inputs:
current_index: Current index from FilePathsForLoop (required, force input)iteration_data: Optional output from FilePathsEnsureExecution or any node in the loop
Outputs:
total_iterations: Total number of iterations completed
Usage: This is the termination point of your loop. Connect the current_index from FilePathsForLoop (or through other nodes) to ensure the loop completes. If you need to ensure multiple nodes execute, connect them to FilePathsEnsureExecution first, then connect FilePathsEnsureExecution's output to this node's iteration_data input.
FilePathsEnsureExecution
Ensures connected nodes execute during loop iteration by collecting their outputs.
Inputs:
input_1throughinput_5: Optional inputs (supports any type with wildcard*)
Outputs:
passthrough: The first non-None input value
Usage: Connect outputs from any nodes that need to execute within the loop to this node's inputs. Then connect this node's output downstream (e.g., to FilePathsLoopClose) to ensure all connected nodes complete execution. If you need more than 5 inputs, chain multiple FilePathsEnsureExecution nodes together - connect the first node's output to one of the second node's inputs.
How ComfyUI List-Based Loops Work
When a node has OUTPUT_IS_LIST = True:
- The node returns lists:
["file1.png", "file2.png", "file3.png"] - ComfyUI automatically executes all downstream nodes once per list item
- Each execution receives one item from the list, not the whole list
- Iteration 1: downstream nodes receive
"file1.png" - Iteration 2: downstream nodes receive
"file2.png" - Iteration 3: downstream nodes receive
"file3.png"
This is ComfyUI's native batching/looping mechanism.
⚠️ Memory Management Warning
Important: The simple loop mechanism in this node pack iterates by node execution, not by iteration cycle. This means all nodes in the loop execute for each file, which can lead to Out of Memory (OOM) errors if proper memory management is not implemented, especially when processing large batches or using memory-intensive operations.
Recommendations:
-
For Node-Based Loops (using FilePathsForLoop):
- Use clean VRAM or clean RAM nodes after heavy procedure nodes complete (e.g., after any model loading required nodes like image generation, audio generation, or video generation)
- This ensures proper cleaning and unloading of unneeded models between iterations
- Example: After your image generation node, connect a "Clean VRAM" or "Clean RAM" node before the next iteration
-
For Sequential Loops (true iteration cycles):
- Recommended: Use third-party for-loop/while-loop custom nodes for sequential iteration
- Use
LoadFilePathsFromFoldernode to get file count (as loop count) and file names data - Pass the file count to your third-party loop node to set the iteration count
- Within the loop, connect the current index value to
GetPathByIndexnode - Connect filenames/filepaths from
LoadFilePathsFromFoldertoGetPathByIndexnode's inputs - This approach provides better control over memory management and true sequential processing
Example Workflows
Simple Loop Processing
- FilePathsForLoop - Set folder path and extension (e.g., "C:/images", "*.png")
- Connect
filepathto your image loader node (e.g., Load Image) - Connect your processing nodes (e.g., filters, effects)
- Connect the final processing output to FilePathsEnsureExecution inputs
- Connect FilePathsForLoop's
current_indexto FilePathsLoopClose'scurrent_index - (Optional) Connect FilePathsEnsureExecution's output to FilePathsLoopClose's
passthrough_1
This setup will automatically process each file in the folder sequentially.
Why Three Nodes?
- FilePathsForLoop: Initiates the loop, provides file paths and metadata
- FilePathsEnsureExecution: Optional helper to chain dependencies and ensure execution order
- FilePathsLoopClose: Terminates the loop, provides completion signal, collects results
Installation
Place this folder in your ComfyUI custom_nodes directory and restart ComfyUI.