Nodes/Comfyui_PDuse/PD_ReplaceWord
ComfyUI Node

PD_ReplaceWord

Find a word in filenames, swap it out — PD_ReplaceWord is your rename bat

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD_ReplaceWord
    • show
    directory_pathC:\\path\\to\\your\\files
    file_formattxt
    search_keyword
    replace_word

    Rename a folder full of files from inside ComfyUI

    PD_TxtBatchRename, shown in the UI as PD_ReplaceWord, is a bulk filename renamer: point it at a directory, give it a keyword to find and a word to replace it with, and it renames every matching file. Leave the replacement empty and it deletes the keyword from names instead. You can scope it by extension (jpg, png, mp3, txt, or all), and it returns a full report of what changed, what didn't, and what was skipped.

    This is dataset-hygiene work - the same filename conventions the KB's image-io-metadata.md describes for training sets, where a 1_R.png must pair with 1_R.txt and the naming is the data structure. When your convention changes (swap _R for _train, drop a _temp suffix from fifty files), this is the node that saves you an hour of rename commands in a terminal you'd rather not open.

    How it works

    The source is a loop over the directory's files. It normalizes the path with os.path.normpath (so C:\path examples don't break on Linux), picks files by extension via a glob (or everything, if all), and for each file whose name contains search_keyword it does a plain str.replace to build the new name. Before renaming it checks whether the target name already exists - if it does, that file is skipped with a warning rather than overwritten. Everything gets a status line in the show output.

    Two important details: it renames in place (no copy, no undo), and it only looks at the top level of the folder - no recursion into subdirectories.

    Inputs and outputs

    • directory_path - the folder (absolute path; the default is a Windows-style placeholder, replace it).
    • file_format - which extensions to touch: jpg, png, mp3, txt, or all.
    • search_keyword - the text to find in filenames. Empty = error.
    • replace_word - the replacement. Empty = delete the keyword.
    • Output: show - a report string with per-file status. Wire it to a Show Text node.

    Installing it

    Part of Comfyui_PDuse:

    cd ComfyUI/custom_nodes
    git clone https://github.com/7BEII/Comfyui_PDuse.git
    cd Comfyui_PDuse
    pip install -r requirements.txt
    

    Or via ComfyUI Manager (search "Comfyui_PDuse") and restart.

    Where it'll bite you

    It's destructive-by-default, so run it once on a copy or a couple of test files before committing. The replacement is naive string replacement - "R" will happily match inside "Render" and "arrow", so longer, distinctive keywords are safer. And because it only scans the top level, nested folders are completely invisible to it - if your dataset is organized in subfolders, this won't touch them, which is either a relief or a disappointment depending on what you wanted. The skip-if-target-exists behavior is a genuine safety win: it refuses to clobber, which is more than most rename tools offer.

    CategoryPD Custom Nodes

    Inputs (4)

    NameTypeDefaultDescription
    directory_pathSTRINGC:\\path\\to\\your\\files
    file_formatCOMBOtxt5 options: jpg, png, mp3, txt, all
    search_keywordSTRING
    replace_wordSTRING

    Outputs (1)

    NameTypeDescription
    showSTRING