# Define folder and replacement strings
folder_path <- "scripts"   # <-- change this
old_string  <- "OLD_STRING"            # <-- what you want to replace
new_string  <- "NEW_STRING"            # <-- replacement

# Construct regex: \b marks word boundaries
pattern <- paste0("\\b", old_string, "\\b")

# List all R script files in the folder
files <- list.files(folder_path, pattern = "\\.R$", full.names = TRUE)

# Loop through each file and perform replacement
for (f in files) {
  # Read file
  txt <- readLines(f, warn = FALSE, encoding = "UTF-8")
  
  # Replace only exact matches
  txt_new <- gsub(pattern, new_string, txt, perl = TRUE)
  
  # Write back to file (overwrite)
  writeLines(txt_new, f, useBytes = TRUE)
}

cat("Exact-match replacement done in", length(files), "files.\n")
