R Code to change file names, remove number prefix; add number prefix.

Main idea is this:

base:: comands file.rename() file.create() basename() dirname() list.dirs() dir()

knitr::opts_chunk$set(echo = TRUE,  
                                            comment="      ##",  
                                            error=TRUE, 
                                            collapse=TRUE) 
library(jimTools) 
# if user provides NO pattern, return all files
# else use the pattern

get_files  <- function(path = NULL, pattern = NULL) {
  ## tests
  list.files(path, pattern)
}
path  <- "~/mp3_files"


path  <- "."
pattern  <- NULL
l  <- list.files(path = path, pattern = pattern)
the_files  <- get_files(path=path, pattern = pattern)
identical(l, the_files)
l


save_old_names  <- function(the_files = NULL) {
  e  <<- new.env()
  e$the_files  <- the_files
}
save_old_names(the_files)
OLD  <- the_files
OLD

remove_prefix  <- function(the_files = NULL, pattern = pattern) {
  sub(pattern=pattern, replace="", x=the_files)
}
the_files  <- remove_prefix(the_files, pattern="")
the_files

## propose new prefix, min of digits
  get_prefix  <- function(n=NULL, digits=4) {
    format  <- paste0("%0",digits,"i_")
    the_prefixs  <- sprintf(format, 1:n)
  }
n  <- length(the_files)
prefix  <- get_prefix(n=n, digits=5)
prefix


get_proposed_name  <- function(the_files = NULL, prefix=NULL) {
  paste0(prefix, the_files)
}
NEW  <- get_proposed_name(the_files = the_files, prefix=prefix)
NEW
# change on disk
OLD_NAMES
NEW_NAMES
#base::file.rename( OLD_NAMES, NEW_NAMES)


##  # groups, "\1\2\3"  but R  wants double:  "\\1\\2\\3"
##  new  <- gsub(pattern = pat, replace="\\1\\2\\3", x = old)
##  new  <- gsub(pattern = pat, replace="\\2_\\3_\\1", x = old)



# caution:
rename_files  <- function() 
 {
    start_time <- Sys.time()

#    file.rename(
                from = paste0(the_dir, old),
                to = paste0(the_dir, new)
                )
    end_time <- Sys.time()
    print (end_time - start_time)
 }

patterns

#  Choose pattern, 
list.files("rmd", full.names= T, pattern="*.Rmd")
list.files("./rmd", pattern="*.Rmd")
list.files("./rmd", full.names = TRUE ,pattern="*.Rmd")
pat  <-  "^[:digit:]{4,6}"
pat  <-  `^[[:digit:]]{4,6}`
pattern=  "^[0-9]*"
pat  <-  `'^_'`
pat  <- `'^_00056'`
pat  <- "^_[[:digit:]]{5}"
pat  <-  "^_[[:digit:]]{5,6}"
pat  <-  "^[[:digit:]]{4,6}"
pat  <-  "^_0[[:digit:]]{4,6}"
pat  <- "^_NA"
pat  <- "^NA"
pat  <-  "^NA[[:digit:]]{4,6}"
pat  <- "_NA_"
pat  <- "^__"
pat  <- "__+"    # + = 1 or more of SECOND _
pat  <- "\\s+"    # 1 or more
pat  <- "_._"   # any character between two '_'
pat  <- "_.ogg"
pat  <- "_\\."  # _ followed by literal .
pat  <- "'"
pat  <- "-"
pat  <- ",_"
#  match 06_Apr_2018
pat  <-  "[[:digit:]]{2}_[[:alpha:]]{3}_[[:digit:]]{4}"
pat  <-  "([[:digit:]]{2})_([[:alpha:]]{3})_([[:digit:]]{4})"
# match 2018_04_06
pat  <-  "([[:digit:]]{4})_([[:digit:]]{2})_([[:digit:]]{2})"

sprintf has some nice features!

sprintf("hello %s", "jim")

sprintf("hello %s", 23)

sprintf("hello %04s", 23)         # min of 4
sprintf("hello %04f", 23)         # 23.000000

sprintf("hello %04i", 23)         # int, min of 4 digits
{

file <- "0010_clean_file_names_use_base.Rmd"
file  <- basename(file) 
file  <- here("rmd", file)
file
}


rmarkdown::render(file, output_format="pdf_document",
                  output_dir="~/Downloads/print_and_delete")


jimrothstein/pkg_mp3 documentation built on July 5, 2021, 7:46 a.m.