filesFindReplace: Find and Replace Text in Files in a Directory or File List

View source: R/utilsFiles.R

filesFindReplaceR Documentation

Find and Replace Text in Files in a Directory or File List

Description

This function searches for specified patterns in files within a directory or a given list of files and replaces them with given text. While optimized for R scripts, the function call fileFindReplace function that can work on any text-based files or xlsx Files.

Usage

filesFindReplace(
  filename,
  pattern,
  replacement,
  word = FALSE,
  ignore.case = FALSE,
  listonly = FALSE
)

Arguments

filename

Character or character vector. The path of the directory, a single file, or a list of files.

pattern

Character vector. A vector of patterns to search for in the files.

replacement

Character vector. A vector of replacements for the patterns found.

word

Logical. If TRUE, only whole words will be replaced. Defaults to FALSE.

ignore.case

Logical. If TRUE, the function performs a case-insensitive search. Defaults to FALSE.

listonly

Logical. If TRUE, the function only lists the files that would be modified without actually modifying them. Defaults to FALSE.

Details

The function supports case-sensitive or case-insensitive searches and can either perform the replacements or just list the files that would be modified.

Value

Returns list: names of files assessed = number of changes. The function modifies the files in-place, or lists them if listonly = TRUE.

See Also

fileFindReplace, xlsxFindReplace,txtFindReplace

Examples

## Not run: 
filesFindReplace("path/to/directory", "oldText", "newText")
filesFindReplace("path/to/file.txt", "oldText", "newText")
filesFindReplace(c("file1.txt", "file2.csv"), "oldText", "newText")

## End(Not run)

Epiconcept-Paris/STRAP-epiuf documentation built on Aug. 5, 2024, 3:41 a.m.