Reputation: 23
I'm trying to rename a number (179) of files which names follow the pattern:
"104-jacques_brel-le_diable_(ca_va)-just.mp3 TEST"
...
"1517-jacques_brel-la_cathedrale-just.mp3 TEST"
Aiming at an output looking like:
"Le diable (ca va).mp3"
...
"La cathedrale.mp3"
Problem is I only know of the paste() command...so I could successfully (and stupidly) add the TEST to the name using:
filenames <- list.files()
filenames[180:181]
filenames <- filenames[-c(180:181)]
files_4dgts<-filenames[c(1:14,21:30,35:80)]
files_3dgts<-filenames[-c(1:14,21:30,35:80)]
for (i in 1:length(files_3dgts))
file.rename(files_3dgts[i],paste(files_3dgts[i],"TEST",sep=""))
for (i in 1:length(files_4dgts))
file.rename(files_4dgts[i],paste(files_4dgts[i],"TEST",sep=""))
Which obviously isn't any kind of solution to my problem.
Upvotes: 2
Views: 123
Reputation: 15395
music <- c("104-jacques_brel-le_diable_(ca_va)-just.mp3 TEST",
"1517-jacques_brel-la_cathedrale-just.mp3 TEST")
# Remove all the digits and irrelevant words
musicwipe <- gsub("[[:digit:]]*-jacques_brel-|-just| TEST", "", music)
# Replace all underscores with spaces
musicspace <- gsub("_", " ", musicwipe)
# Replace first letter with uppercase letter
musicupper <- sub("^([[:alpha:]])", "\\U\\1", musicspace, perl=TRUE)
#Result
musicupper
[1] "Le diable (ca va).mp3" "La cathedrale.mp3"
Upvotes: 4