Skip to content
Back to formatted view

Raw Message

Message-ID: <CA+8X3fWPqsXnEwDn_t2p8roOFNeBCkAMtoKDtgknJL20BoR_-Q@mail.gmail.com>
Date: 2017-09-28T10:25:59Z
From: Jim Lemon
Subject: rename multiple files by file.rename or other functions
In-Reply-To: <CABcx46Aj712=szdjCCWvgpm-6srRt4uRA8q3vYPvsHSNWSAAbw@mail.gmail.com>

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.