nirgrahamuk is right that R uses regex to match file names.
Here's my suggestion for the full solution:
# Creating files for the example
# Two that should be renamed, one that shouldn't
files <- file.path(
tempdir(),
c("Carrie_test_mid.csv", "John_test.csv", "do_not_match.csv")
)
for (ff in files) {
writeLines("bah humbug", ff)
}
## Find all files matching the pattern
test_files <- list.files(
path = tempdir(), # replace with the directory you want
pattern = "test.*\\.csv$", # has "test", followed by 0 or more characters,
# then ".csv", and then nothing else ($)
full.names = TRUE # include the directory in the result
)
length(test_files)
# [1] 2
test_files[1]
# [1] "C:\\Users\\nwerth\\AppData\\Local\\Temp\\RtmpC4hJTv/Carrie_test_mid.csv"
I wish full.names = TRUE
was the function's default, but oh well.
Next, we can use a function from the tools
package (which comes with every R installation) to remove file extensions from paths. This makes it easy to tack things onto the end of file names.
Also, if you're not familiar with the sprintf
, I'd recommend trying it out. It's a nice way to build strings with guaranteed formatting and doesn't require nested paste0
and format
calls. For now, just know that %s
means "insert a string" and %d
means "insert a whole number."
## Create the new names
no_file_ext <- tools::file_path_sans_ext(test_files)
new_names <- sprintf("%s_%d.csv", no_file_ext, seq_along(no_file_ext))
new_names[1]
# [1] "C:\\Users\\nwerth\\AppData\\Local\\Temp\\RtmpC4hJTv/Carrie_test_mid_1.csv"
Now we can just use R's file.rename
function, which is helpfully vectorized.
## Rename the files
file.rename(test_files, new_names)
# [1] TRUE TRUE
list.files(path = tempdir())
# [1] "Carrie_test_mid_1.csv" "John_test_2.csv" "do_not_match.csv"