vim:linebreak:nospell tw=81 fo=tqlnr foldcolumn=3

title: Template for .Rmd output: pdf_document: latex_engine: xelatex toc: TRUE toc_depth: 1 fontsize: 12pt geometry: margin=0.3in,top=0.25in TAGS: clean


LEGACY: do not use b/c use fs::, stringr::

Easier to use base:: commands, not fs::

:hi Comment ctermfg=36 :hi Comment ctermfg=102 (white, brighter)

PURPOSE: Read file names, renumber, clean file names

knitr::opts_chunk$set(echo = TRUE,  
                                            comment="      ##",  
                                            error=TRUE, 
                                            collapse=TRUE)

library(jimTools)
load_all(".")
library(tibble)
library(dplyr)

Construct path

p  <- "~/Downloads/mp3/from_youtube"

# capture directory info, remove full path
t  <- fs::dir_info(p, recurse = FALSE) %>%
  filter(type == "file" ) %>%
    mutate(OLD =fs::path_file(path)) %>% 
  arrange(desc(size)) %>%
  select(size, OLD)
head(t, n=10)

backup

# backup
saveRDS(t,file = here("data","original_tibble.RDS")) 
# keep live spare
z  <- t

read from backup

t  <- readRDS(file = here("data", "original_tibble.RDS"))
head(t)
# safer way to test
length(t$OLD)
t$OLD[stringr::str_detect(t$OLD, "^[:digit:]{4,6}")]

# some "_nnnnn", where n=digit
pat  <-  "^_[:digit:]{5}"
t$OLD[stringr::str_detect(t$OLD, "^_[:digit:]{5}")}

remove old prefix number

t  <-   t %>% 
        dplyr::mutate(NEW = stringr::str_remove(OLD,  "^[:digit:]{4,6}"))
t$NEW

PAUSE


remove "^NA"

t  <- t %>% mutate(NEW = 
    stringr::str_remove(NEW, "^_NA_")) %>% select(NEW, OLD)
head(t$NEW, n=20)

ToDO

# ToDO
# - remove funny symbols:  #,~,non-ascii, quotes 

remove leading "_"

t  <- t %>% mutate (NEW =
stringr::str_remove(NEW, "^_"))

head(t$NEW)

# replace multiple "_" by single "_"
# ====================================

# replace whitespace with '_'
t  <- t %>% mutate(NEW = 
stringr::str_replace_all(NEW, "\\s{1,}","_"))

t %>% select(NEW)
# 


# remove any single quotes
t  <- t %>% mutate(NEW = stringr::str_replace_all(NEW, "'",""))

# replace pattern '_-_' with '_'
t  <- t %>% mutate(NEW =
stringr::str_replace(NEW,"_-_","_"))
head(t$NEW)

# replace multiple '_' with single '_'
t<- t %>% mutate(NEW = stringr::str_replace_all(NEW, "_+", "_") ) 
head(t$NEW)


# remove any ending "_", such as .ogg_
t  <- t %>% mutate(NEW =
stringr::str_replace(NEW,"_$",""))


# replace '_.ogg' with '.ogg', but '.' is special:  use '\\.'
t  <- t %>% mutate(NEW =
stringr::str_replace(NEW,"_\\.ogg$",".ogg"))

head(t$NEW)
length(t$NEW)
head(t)

START HERE

# next TO_DO
# replace non punch [^[:punct:]] and not letters, numbers \\W

# use last_clean 
t  <- mp3_tibble_propose_new_name(t) 
head(t$OLD, n=5)
head(t$NEW, n=5)

## uncomment, for real!
# t  <- mp3_tibble_move_files(t,p)

FINAL: Move files on disk

fs::file_move(fs::path(p,t$OLD), fs::path(p,t$NEW))

FUTURE 007A_mark_play_files to delete

file_jr_proposed_delete <- function(OLD){
    OLD <- fs::path(p,OLD)
    TEMP <- stringr::str_c(OLD,".DELETE")
    fs::file_move(OLD,TEMP)
}

#2  remove TEMP
# clean, pooof:  all gone.
file_jr_proposed_delete("000001_test")

FUTURE

012_fs: glob, file Lengths

# file list
v <- dir_ls(glob = "*.Rmd")  # returns character vector
length(v)
#
t <- dir_ls(glob="*.Rmd") %>% map_df(length, .id="file")
t

USELESS

{ writeLines("abc") writeLines("$abc") writeLines("^abc") writeLines("a\.bc") }

knitr::knit_exit()

file <- "052_clean_mp3_file_names.Rmd"
file  <- basename(file)
dir="rmd"

jimTools::ren_pdf(file,dir)
jimTools::ren_github(file, dir)


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