How do I find all the files with name containing certain word and re-name these files?

Hi

I am new to R.

In student folder, I want a list of all the CSV files containing the word "test". I used the below code but obtained characters (0), but I do have some CSV files with "test" in the folder:

list<-list.files('C:/Desktop/student', pattern="*test*.csv", full.names=TRUE)

Next, for these files, I want to rename them by adding _number in the back.
Eg
Original file name:
Carrie_test_mid.csv
John_test.csv

Renamed File:
Carrie_test_mid_1.csv
John_test_2.csv

How do I solve the above?

Thank you

expect string matching functions in R to work as regex pattern matchers, the windows/dos style wildcards are not used.
something like

list<-list.files('C:/Desktop/student', pattern="(test)+(.csv)", full.names=TRUE)

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"

Hi [nirgrahamuk],

Many thanks for the detailed answer. But if instead of appending numbers to the new file name, how do I append the date-time stamp?

Thank you so much