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

peter dalgaard pdalgd at gmail.com
Thu Sep 28 13:32:47 CEST 2017


> On 28 Sep 2017, at 12:57 , Eric Berger <ericjberger at gmail.com> wrote:
> 
> Hi John,
> Thanks to Jim for pointing out the file.rename() function. You can try this:
> 
> # define the filename templates
> strIn  <- "XYZW--Genesis_ABC.mp3"
> strOut <- "01Gen--.mp3"
> 
> # create the strings "01", "02", ..., "50"
> v <- sapply(1:50, function(i) sprintf("%02d",i) )
> 
> # perform all the file renames
> for ( s in v ) { file.rename(sub("--",s,strIn), sub("--",s,strOut) ) }
> 
> HTH,
> Eric
> 
> 

I think you can even do this:

strIn <- sprintf("XYZW%02dGenesis_ABC.mp3", 1:50)
strOut <- sprintf("01Gen%02d.mp3", 1:50)
## perhaps try cbind(strIn,strOut) first
file.rename(strIn, strOut)

-pd

> 
> 
> 
> 
> 
> 
> 
> On Thu, Sep 28, 2017 at 1:25 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
> 
>> Hi John,
>> Maybe this:
>> 
>> filenames<-c("XYZW01Genesis_ABC.mp3","XYZW02Genesis_ABC.mp3")
>> for(filename in filenames) {
>> filefirst<-sapply(strsplit(filename,"[.]"),"[",1)
>> fileno<-sub("_","",gsub("[[:alpha:]]","",filefirst))
>> file.rename(filename,paste("01Gen",fileno,".mp3",sep=""))
>> }
>> 
>> Jim
>> 
>> On Thu, Sep 28, 2017 at 7:37 PM, John <miaojpm at gmail.com> wrote:
>>> Hi,
>>> 
>>>   I have 50 files whose names are
>>> 
>>> XYZW01Genesis_ABC.mp3
>>> XYZW02Genesis_ABC.mp3
>>> .......
>>> XYZW50Genesis_ABC.mp3
>>> 
>>>   As you can tell, the only difference across the files are 01, 02,
>>> 03,....50.
>>> 
>>>   I would like to rename them to
>>> 01Gen01.mp3
>>> 01Gen02.mp3
>>> .......
>>> 01Gen50.mp3
>>> 
>>>  If I store them in one folder and write an R code in that folder, how
>> can
>>> it be done?
>>> 
>>>   Thanks,
>>> 
>>> John
>>> 
>>>        [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/
>> posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/
>> posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list