Rename Files and Directories Containing Specific Text
Source:R/ngr_str_replace_filenames.R
ngr_str_replace_filenames.RdThis function renames files and directories by replacing occurrences of a specified text with a new text. It allows exclusions and inclusions using glob patterns and provides an option to confirm changes before applying them.
Usage
ngr_str_replace_filenames(
path = ".",
text_current,
text_replace,
glob_exclude = ".git",
glob_include = NULL,
ask = TRUE
)Arguments
- path
character A vector of one or more paths. The root directory to start searching for files and directories.
- text_current
character A string representing the text to be replaced in filenames.
- text_replace
character A string representing the new text to replace the current text in filenames.
- glob_exclude
character A glob pattern to exclude specific paths from renaming. Default is ".git".
- glob_include
character or NULL A glob pattern to include specific files or directories. Default is
NULL.- ask
logical Whether to prompt the user for confirmation before renaming. Default is
TRUE.
Details
The function uses stringr::str_detect() to find filenames containing the specified text_current and excludes those where text_replace is already present. It then renames files and directories accordingly using fs::file_move().