Package: fs


Function: file_copy()


1. Copy “summary_obs.xlsx” from “obs_2022-03-15” to another existing folder (“all_obs”).

Let’s review the current files in both folders.

Files in “obs_2022-03-15”

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

Files in “all_obs”.

You’ll see there are currently no files in this folder.

base::basename(fs::dir_ls(here::here("file-system", "all_obs")))
character(0)

Now let’s copy the “summary_obs.xlsx” file.

  1. We grab our existing file names and put them in the object file_to_copy
  2. We put our new directory in the object new_directory
  3. Then we copy the files
  • Note: We use the argument glob = “*summary*” to grab the file with the word “summary” in it. We could have also chosen to use the regexp argument instead.
file_to_copy <- fs::dir_ls(here("file-system", "obs_2022-03-15"), glob = "*summary*")

new_directory <- here::here("file-system", "all_obs")
    
fs::file_copy(path = file_to_copy , new_path = new_directory)

Let’s view the files in “all_obs” now.

base::basename(fs::dir_ls(here::here("file-system", "all_obs")))
[1] "summary_obs.xlsx"

2. Copy all “summary_obs.xlsx” from any folders named “obs_YYYY-MM-DD” to another existing folder (“all_files”).

First let’s view how many folders named “obs_YYYY-MM-DD” exist in our directory. We will need to change our directory to be a level higher than in the previous example (up to the “file-system” folder).

base::basename(fs::dir_ls(here::here("file-system")))
[1] "all_files"      "obs_2022-03-15" "obs_2022-03-16"

We notice we have 2 date folders currently.

  1. First, we can grab the files we want to move over and put them into an object file_to_copy_all.
  • Note: We use the argument glob = “*summary*” to grab the files with the word “summary” in it.

  • Note: I am using the argument recurse = TRUE to pull all of the summary files.

file_to_copy_all <- fs::dir_ls(here("file-system"), glob = "*summary*",
                     recurse = TRUE)

Let’s see what files we grabbed.

base::basename(file_to_copy_all)
[1] "summary_obs.xlsx" "summary_obs.xlsx"
  1. Next we need to denote where our new directory is.

If our files had unique names, this would be pretty straightforward.

new_directory_all <- here::here("file-system", "all_files")

However, above you can see that the files in each date folder are identically named so we will have duplicate file names which are not allowed.

To differentiate between the files once we copy them over we are going to add the suffix “_YYYY-MM-DD” to each file.

We first need to extract the date from the folder using stringr::str_extract_all() and providing the regex argument 4 digits - 2 digits - 2 digits which is the format of our date.

folder_date <- stringr::str_extract_all(file_to_copy_all, "\\d{4}-\\d{2}-\\d{2}")

Then to create the new file path, we can grab the basename of the old filepath, and replace the “.xlsx” portion of the filename (using stringr::str_replace()) with “_“, the folder date, and”.xlsx”, all pasted together using base::paste0() which says paste everything together with no separator.

filename <- base::basename(file_to_copy_all)

new_directory_all <- here::here("file-system", "all_files", stringr::str_replace(
  filename, ".xlsx", 
  base::paste0("_", folder_date, ".xlsx")))
  1. Last we will need to copy the files over.
  • Note: If you want to run this syntax again to move over future files, you can add the argument overwrite = TRUE to overwrite existing files in the folder.
fs::file_copy(path = file_to_copy_all , new_path = new_directory_all)

Let’s see what files we have in our “all_files” folder now.

base::basename(fs::dir_ls(here::here("file-system", "all_files")))
[1] "summary_obs_2022-03-15.xlsx" "summary_obs_2022-03-16.xlsx"

Note: If you have previously pulled in “summary_obs.xlsx” files into your “all_files” folder (from say earlier date files), those files will be pulled in again so you will need to think about subsetting those files in your step #1.

You can do this by adding stringr::str_subset() and telling it to remove any file paths that contain the string “all_files” using the argument negate = TRUE.

file_to_copy_all <- fs::dir_ls(here("file-system"), glob = "*summary*",
                     recurse = TRUE) %>%
  stringr::str_subset("all_files", negate = TRUE)

Return to File System