Package: fs


Function: file_move()


1. Rename one file (“summary_obs.xlsx”) in the folder “obs_2022-03-15”.

Let’s review the current files

base::basename(fs::dir_ls(here::here("file-system", "obs_2022-03-15")))
[1] "obs_a.xlsx"       "obs_b.xlsx"       "obs_c.xlsx"       "summary_obs.xlsx"

Rename “summary_obs.xlsx” to “summary_tch_obs.xlsx”

fs::file_move(path = here::here("file-system", "obs_2022-03-15", "summary_obs.xlsx"),
              new_path = here::here("file-system", "obs_2022-03-15", "summary_tch_obs.xlsx"))

Let’s review the files now

base::basename(fs::dir_ls(here::here("file-system", "obs_2022-03-15")))
[1] "obs_a.xlsx"           "obs_b.xlsx"           "obs_c.xlsx"          
[4] "summary_tch_obs.xlsx"

2. Rename multiple files in the folder “obs_2022-03-15”.

Let’s review the current files

base::basename(fs::dir_ls(here::here("file-system", "obs_2022-03-15")))
[1] "obs_a.xlsx"       "obs_b.xlsx"       "obs_c.xlsx"       "summary_obs.xlsx"

Rename obs_a, obs_b, and obs_c to obs1, obs2, obs3.

  1. First let’s grab the files we want to rename and put them in an object “files_old”.

I could definitely write out all of the file names and put them into a vector, like this.

files_old <- c(here::here("file-system", "obs_2022-03-15","obs_a.xlsx"),
               here::here("file-system", "obs_2022-03-15","obs_b.xlsx"),
               here::here("file-system", "obs_2022-03-15","obs_c.xlsx"))

But if I had tons of files, this would take forever. We’ve already seen that fs::dir_ls() grabs the names of all of our files in our directory so we can use this function to more efficiently grab our existing files.

Since there is a “summary_obs.xlsx” file that I do not want to rename, I am going to add the fs::dir_ls() argument glob here which allows you to grab files/folders using a globbing pattern. That pattern is passed to base::grep() to filter paths. The “?” in globbing matches one character.

files_old <- fs::dir_ls(here("file-system", "obs_2022-03-15"), glob = "*obs_?.xlsx")

You could instead use the regexp argument and use a regular expression pattern to grab the files you want. This pattern is also passed to base::grep() to filter paths. The “[a-z]” in regex matches one alphabetic character.

files_old <- fs::dir_ls(here("file-system", "obs_2022-03-15"), regexp = "*obs_[a-z].xlsx")

If I wanted to grab all files, I could simply write the code like this.

files_old <- fs::dir_ls(here::here("file-system", "obs_2022-03-15"))
  1. Now let’s create the new names for our files and put them in an object “files_new”.

Again, I could write out the new names for all of our files but if we are renaming many files, we should find a more efficient way to rename.

  • Note: I am using base::paste0() to paste the word “obs” with the file number and “.xslx”.
  • Note: I am using base::length() to create numbers for the number of files that exist in the folder.
files_new <- here::here("file-system", "obs_2022-03-15",(base::paste0( "obs", 1:length(files_old), ".xlsx")))
  1. Now let’s rename the files
fs::file_move(path = files_old, new_path = files_new)

Let’s view the new names.

base::basename(fs::dir_ls(here::here("file-system", "obs_2022-03-15")))
[1] "obs1.xlsx"        "obs2.xlsx"        "obs3.xlsx"        "summary_obs.xlsx"

If I didn’t want to create new names from scratch but simply wanted to replace a part of the existing name, I could consider something like this instead, using stringr::str_replace() to replace any “_letter” with the file number.

files_old <- fs::dir_ls(here::here("file-system", "obs_2022-03-15"), glob='obs_*')

file_number <- as.character(1:length(files_old))
  
files_new <- stringr::str_replace(files_old, "_[a-z]", file_number)
  
fs::file_move(path = files_old, new_path = files_new)

Return to File System