rename multiple files by file.rename or other functions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

rename multiple files by file.rename or other functions

miao
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: rename multiple files by file.rename or other functions

Ulrik Stervbo-2
Hi John,

I don't know how to do this with R, but on Linux I'd use rename (or maybe
even by hand if it's a one time event). On Windows I believe there is a
tool called Bulk Rename.

HTH
Ulrik

On Thu, 28 Sep 2017 at 11:37 John <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: rename multiple files by file.rename or other functions

Thierry Onkelinx
In reply to this post by miao
The combination of list.files(), gsub() and file.rename() should to the
trick.


ir. Thierry Onkelinx
Statisticus/ Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
[hidden email]
Kliniekstraat 25, B-1070 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

[image: Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging
in Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis. Vanaf
dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.]
<https://overheid.vlaanderen.be/mobiliteitsplan-herman-teirlinckgebouw>
Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in
Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis.
Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000
Brussel.

///////////////////////////////////////////////////////////////////////////////////////////
<https://www.inbo.be>

2017-09-28 11:37 GMT+02:00 John <[hidden email]>:

> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: rename multiple files by file.rename or other functions

Jim Lemon-4
In reply to this post by miao
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: rename multiple files by file.rename or other functions

Eric Berger
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









On Thu, Sep 28, 2017 at 1:25 PM, Jim Lemon <[hidden email]> 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 <[hidden email]> 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]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: rename multiple files by file.rename or other functions

Peter Dalgaard-2

> On 28 Sep 2017, at 12:57 , Eric Berger <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: rename multiple files by file.rename or other functions

Jim Lemon-4
In reply to this post by Thierry Onkelinx
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

______________________________________________
[hidden email] 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.