[R] rename multiple files by file.rename or other functions

Jim Lemon drjimlemon at gmail.com
Fri Sep 29 00:22:28 CEST 2017


Hi John,
After a bit of thinking:

# fill in the appropriate path and pattern
filenames<-list.files(path=???,pattern=???)
for(filename in filenames) {
 filefirst<-sapply(strsplit(filename,"[.]"),"[",1)
 # delete all non-digits
 fileno<-gsub("[^[:digit:]]","",filefirst)
 file.rename(filename,paste("01Gen",fileno,".mp3",sep=""))
}

Jim



More information about the R-help mailing list