2

我正在尝试重命名一些(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=""))

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

4

1 回答 1

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" 
于 2013-03-20T11:49:56.973 回答