Use base:: to copy files between devices.

base:: files and dir commands

Collect HERE

Includes, base commands to rename files in directory.

base:: commands (why use fs:: ?) base:: comands file.rename() file.create() file.copy() basename() dirname() list.dirs() dir()

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

Local (Laptop)

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

# fake pat (does not exisit)
pat  <-  "^_[:digit:]{5}"

list.files("./rmd", pattern="*.Rmd")
list.files("./rmd", full.names = TRUE ,pattern="*.Rmd")

Choose Device

ONEPLUS

# ONEPlus
# do NOT play with Android Developer's options

# (do not use ~/my_oneplus/)

# DO run jmtpfs ~/my_oneplus3
# Check using ll ~/my_oneplus3

>   jmtpfs ~/my_oneplus/
>   the_dir  <- "~/my_oneplus/'Internal shared storage'/"
the_dir  <- "~/my_oneplus/Internal shared storage/Music/"
list.files(path = the_dir)

Google Drive

the_dir  <- "/var/host/media/fuse/drivefs-34a039d82ac00e8c8d62cd30042f3741/root/" 

USB Stick (32 GB)

the_dir  <- "/media/removable/mp3_music/clean_mp3/"
the_dir
list.files(path = the_dir)

To copy between devices:

Get list of files from device

the_dir

v  <- list.files(path = the_dir)
v

length(v)

# limit to 1000 at a time.

first  <- v[1:100]
length(first)
head(first)


rest  <- v[-(1:100)]
length(rest)
head(rest)

DEVICE to Laptop

# MOVE to laptop
# from must  be character, filename + path
# to: can be new directory only

z  <- paste0(the_dir, first)
z

# SLOW !!!
file.copy(from = z,
          to = "~/Downloads/mp3/from_youtube/",
          overwrite = TRUE)
#system("cp z ~/Downloads/mp3/from_youtube/")
to_dir  <- "/media/removable/mp3_music/clean_mp3/"
to_dir
list.files(path = to_dir)

from_dir  <- "~/Downloads/mp3/from_youtube/"
from_dir
list.files(path = from_dir)

z  <- list.files(path = from_dir)
z

# SLOW
file.copy(from = paste0(from_dir,z),
          to = to_dir,
          overwrite = TRUE)
### 

### Some patterns:
>  pat  <-  `^[[:digit:]]{4,6}`
>  pat  <-  `'^_'`
>  pat <- `'^_00056'`
>  pat <- "^_[[:digit:]]{5}"


### Main idea is this:
* Set dir, 
* Choose pattern to match files
* old = original file names
* new = proposed file names, with changes (using gsub)
* prepend the dir
* file.rename(old, new)

```r
list.files(path = the_dir)
#pat  <-  "^_[:digit:]{5}"
 pat  <-  "^[[:digit:]]{4,6}"
 pat  <-  "^_0[[:digit:]]{4,6}"
pat  <- "^_NA"
pat  <-  "^NA[[:digit:]]{4,6}"
list.files(path = the_dir, pattern = pat)

old <- list.files(path = the_dir, pattern = pat)
old
new  <- gsub(pattern = pat, replace="", x = old)
new

old  <- paste0(the_dir,old)
old

new  <- paste0(the_dir,new)
new

# slow - on android
file.rename(
            from = old, 
            to = new)

Other examples

prepend  <- "0"
old  <- list.files("./rmd", pattern="*.Rmd")
new  <- paste0(prepend, old)
file.move

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

regex, sub(), gsub will work stringr is easier?

the_dir  <- "./rmd"
the_files  <- list.files(the_dir)
the_files
library(stringr)
# regex
the_prefixes  <- stringr::str_extract(the_files, "^[0-9]*[A-Z]?_" )
the_prefixes

R can access files on USB stick!

the_dir  <- "/media/removable/mp3_music/clean_mp3/"
the_files  <- list.files(the_dir)
the_files



pat  <- "^NA"
pat  <-  "^[[:digit:]]{4,6}"

list.files(path = the_dir, pattern = pat)
old <- list.files(path = the_dir, pattern = pat)
old
new  <- gsub(pattern = pat, replace="", x = old)
new

# complete path
old  <- paste0(the_dir,old)
old

new  <- paste0(the_dir,new)
new

# use complete path
file.rename(
            from = old, 
            to = new)

knitr::knit_exit() 

/newpage


/newpage

# TODO:  file is found when knitr runs (see above)

# file must be of form:
# dir/name_of_this_file    where dir is relative to project root

file  <- here("", "")
file

# in general, pdf will look nicer
rmarkdown::render(file,
                  #output_format = "pdf_document",
                  output_format = "html_document",
                  output_file = "~/Downloads/print_and_delete/out")


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