diff --git a/R/copy_folder_contents.R b/R/copy_folder_contents.R new file mode 100644 index 0000000..f536864 --- /dev/null +++ b/R/copy_folder_contents.R @@ -0,0 +1,73 @@ +#' Copy folder and its contents +#' +#' @description +#' Function that makes a copy of all the files and subdirectories from a source folder to a destination folder. +#' If files already exist in the destination folder, the user is prompted to decide whether to overwrite them or not. +#' +#' @param folder_from Character. Path to the source directory containing the files and folders to be copied. +#' @param folder_to Character. Path to the destination directory where files and folders will be copied +#' +#' @return NULL. The function is called for its side effects of copying files. + +copy_folder_contents <- function (folder_from, folder_to){ + + tryCatch( + expr = { + + # Take a list with of the files present in the source and destination folders + files_from <- list.files(folder_from, + recursive = TRUE) + files_to <- list.files(folder_to, + recursive = TRUE) + + + # Create the overwrite variable with FALSE as default value + overwrite <- FALSE + + # Check if there are files in the destination folder + if(any(files_from %in% files_to)){ + + message("WARNING: There are existing files in the destination folder.") + + answer <- "" + + while(!(answer %in% c("Y", "y", "N", "n"))){ + answer <- readline(prompt="Do you want to overwrite existing files? (Y/N): ") + } + + if(answer == "Y" | answer == "y"){ + overwrite <- TRUE + } else { + message("No files were copied.") + return(NULL) + } + + } + + #' Create subdirectories in the destination folder, + #' if they do not exist already + + folders <- list.dirs(folder_from, + recursive = TRUE, + full.names = FALSE) + + lapply(file.path(folder_to, folders), + dir.create, + recursive = TRUE) + + # Copy files from source to destination + file.copy(from = file.path(folder_from, files_from), + to = file.path(folder_to, files_from), + overwrite = overwrite) + + message("Files copied successfully.") + + }, error = function(e){ + + stop("An error occurred while copying files: ", e$message) + + } + ) + +} + diff --git a/rim_post_dump.R b/rim_post_dump.R index 4480868..acff2b2 100644 --- a/rim_post_dump.R +++ b/rim_post_dump.R @@ -67,7 +67,7 @@ source(file.path(PRIVATE_FOLDER_NAME, "user_settings.R")) # FILENAME_TAL <- "IEOUPMUETALSIRENO.TXT" # MONTH: 1 to 12, or vector with month in numbers -MONTH <- c(11) +MONTH <- c(12) # YEAR YEAR <- 2025 @@ -132,7 +132,7 @@ PATH_BACKUP <- file.path(PATH_FILES, BACKUP_FOLDER_NAME) # path to data-raw folder PATH_DATA_RAW <- file.path(getwd(), DATA_RAW_FOLDER_NAME) # path to shared folder -PATH_SHARED_ERRORS <- file.path(PATH_SHARE_FOLDER, YEAR, IDENTIFIER, ERRORS_FOLDER_NAME) +PATH_SHARED_ERRORS <- file.path(PATH_SHARE_FOLDER, YEAR, IDENTIFIER) # path to SIRENO folder. PATH_SIRENO <- "C:/sireno" @@ -232,7 +232,7 @@ errors <- rim_check(muestreos_up) # errors <- rim_check_annual(muestreos_up) # errors <- rim_check_annual_nvdp_matched(muestreos_up) -errors <- list("GS" = errors[["GS"]][errors[["GS"]]$PUERTO == "Muros", ]) +# errors <- list("GS" = errors[["GS"]][errors[["GS"]]$PUERTO == "Muros", ]) # Check oab data dumped in rim: # - sampled type 4, MT2B @@ -262,6 +262,7 @@ rstudioapi::documentSaveAll() sapmuebase::backupScripts(FILES_TO_BACKUP, path_backup = PATH_BACKUP) # SAVE FILES TO SHARED FOLDER -------------------------------------------------- + copy_files_to_folder(PATH_FILES, PATH_SHARED_ERRORS) # SEND EMAILS AUTOMATICALLY ---------------------------------------------------- @@ -281,10 +282,10 @@ copy_files_to_folder(PATH_FILES, PATH_SHARED_ERRORS) # - NOTES: any notes to add to the email. If there aren't, must be set to "". accessory_email_info <- data.frame( AREA_INF = c("AC", "GC", "GN", "GS"), - LINK = c("", - "", - "", - ""), + LINK = c("https://saco.csic.es/index.php/f/694456590", + "https://saco.csic.es/index.php/f/694456585", + "https://saco.csic.es/index.php/f/694456587", + "https://saco.csic.es/index.php/f/694456586"), NOTES = c("", "",