重命名R中的音乐文件

时间:2013-03-20 11:36:15

标签: r file-rename

我正在尝试重命名一个数字(179)的文件名称遵循以下模式:

"104-jacques_brel-le_diable_(ca_va)-just.mp3 TEST"
...
"1517-jacques_brel-la_cathedrale-just.mp3 TEST" 

针对输出看起来像:

"Le diable (ca va).mp3"
...
"La cathedrale.mp3"

问题是我只知道paste()命令...所以我可以成功(并且愚蠢地)使用以下命令将TEST添加到名称:

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=""))

这显然不是我问题的任何解决方案。

1 个答案:

答案 0 :(得分:4)

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"