Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rename_with_dash <- function(dir){
files_to_rename <- list.files(dir,
pattern = "_",
recursive = TRUE,
full.names = TRUE)
files_new_names <- files_to_rename %>%
str_replace_all(pattern = "_", replacement = "-")
walk2(files_to_rename,
files_new_names,
file.rename)
}
library(stringr)
library(purrr)
rename_with_dash <- function(dir){
files_to_rename <- list.files(dir,
pattern = "_",
recursive = TRUE,
full.names = TRUE)
files_new_names <- files_to_rename %>%
str_replace_all(pattern = "_", replacement = "-")
walk2(files_to_rename,
files_new_names,
file.rename)
}
rename_with_dash("modules/module-2/")
rename_with_dash("modules")
renumber_basename <- function(x) {
files_basename <- x %>% basename()
new_basename <- files_basename %>%
str_replace_all("[0-1][0-9]", paste0("0",1:length(.)))
files_new_fullname <- str_replace_all(string = x,
pattern = files_basename,
replacement = new_basename)
files_new_fullname
}
remove_act_prefix <- function(x){
x %>%
str_remove_all(pattern = "Act\\-01\\-Step\\-01\\-") %>%
str_remove_all(pattern = "Act\\-01\\-Step\\-02\\-") %>%
str_remove_all(pattern = "Act\\-01\\-Step\\-03\\-") %>%
str_remove_all(pattern = "Act\\-01\\-Step\\-04\\-") %>%
str_remove_all(pattern = "Act\\-02\\-Step\\-01\\-") %>%
str_remove_all(pattern = "Act\\-02\\-Step\\-02\\-") %>%
str_remove_all(pattern = "Act\\-02\\-Step\\-03\\-") %>%
str_remove_all(pattern = "Act\\-02\\-Step\\-04\\-") %>%
str_remove_all(pattern = "Act\\-03\\-Step\\-01\\-") %>%
str_remove_all(pattern = "Act\\-03\\-Step\\-02\\-") %>%
str_remove_all(pattern = "Act\\-03\\-Step\\-03\\-") %>%
str_remove_all(pattern = "Act\\-03\\-Step\\-04\\-") %>%
str_remove_all(pattern = "Act\\-04\\-Step\\-01\\-") %>%
str_remove_all(pattern = "Act\\-04\\-Step\\-02\\-") %>%
str_remove_all(pattern = "Act\\-04\\-Step\\-03\\-") %>%
str_remove_all(pattern = "Act\\-04\\-Step\\-04\\-")
}
rename_module_step <- function(dir){
files_to_rename <- list.files(dir,
pattern = "^Act",
recursive = TRUE,
full.names = TRUE)
files_new_names <- remove_act_prefix(files_to_rename)
files_new_fullname <- renumber_basename(files_new_names)
walk2(files_to_rename,
files_new_fullname,
file.rename)
}
# rename_module_step("modules/module-6/01-data-management/")
# rename_module_step("modules/module-6/02-data-collection-methods/")
rename_module_step("modules/module-6/03-ethics-data-privacy/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment