-
Notifications
You must be signed in to change notification settings - Fork 0
/
041_change_file_name_EXAMPLE.R
49 lines (40 loc) · 1.31 KB
/
041_change_file_name_EXAMPLE.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
TODO
2024-04-08
- put into function;
- add tmp dir and tests?
## EXAMPLE: if file name is of form dddd and lead d is 0, cut it. ie 0123 becomes 123; but 4123 is left alone
# -----------------
# current setup
# -----------------
# dir <- "~/code/try_things_here/BASE/"
#dir <- "~/code/try_things_here/envir/"
dir <- "~/code/mp3/"
list.files(dir)
# ------------------------------------------------
# files to be changed (0 followed by 4 digits)
# ------------------------------------------------
pat <- "^0[0-9]{3}"
# L= list.files(dir, pattern = pat, full.names=T)
L <- list.files(dir, pattern = pat)
L
# ----------------------------------------------------------------------
# for each such file, make this change (here: remove leading zero)
# ----------------------------------------------------------------------
pat <- "^0"
M <- gsub(x = L, pattern = pat, replacement = "")
M
# ------------------------
# add directory prefix
# ------------------------
L1 <- paste0(dir, L) # OLD list
M1 <- paste0(dir, M) # NEW list
# ---------------------------------------------------
# double check! L1 should be OLD list, M1 should be NEW list
# ---------------------------------------------------
L1
M1
# ------------------------
#* file.rename(old, new)
# ------------------------
file.rename(L1, M1)
list.files(dir)