Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions R/copy_folder_contents.R
Original file line number Diff line number Diff line change
@@ -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)

}
)

}

15 changes: 8 additions & 7 deletions rim_post_dump.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ----------------------------------------------------
Expand All @@ -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("",
"",
Expand Down