Hi, Check ?file.rename() http://stackoverflow.com/questions/10758965/how-do-i-rename-files-using-r A.K. I have list of files in folder e.g p126r62_5t19880613_nn1.tif.gz ? ? ?p126r62_5t19880613_nn2.tif.gz ? ? ?p200r65_5t19880613_nn1.tif.gz and I would like to change file name to ? ? ?LT512606219880613XX_B1.tif.gz ? ? ?LT512606219880613XX_B2.tif.gz ? ? ?LT520006519880613XX_B1.tif.gz Does any body knows how It can be changed?
Of course you can use file.rename() to do this. Can you explain more the changing pattern in you case? Best, Qiang On Mon, Feb 3, 2014 at 10:48 AM, arun <smartpink111@yahoo.com> wrote:> Hi, > Check ?file.rename() > http://stackoverflow.com/questions/10758965/how-do-i-rename-files-using-r > > > A.K. > > > I have list of files in folder > > e.g p126r62_5t19880613_nn1.tif.gz > p126r62_5t19880613_nn2.tif.gz > p200r65_5t19880613_nn1.tif.gz > > and I would like to change file name to > LT512606219880613XX_B1.tif.gz > LT512606219880613XX_B2.tif.gz > LT520006519880613XX_B1.tif.gz > > Does any body knows how It can be changed? > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Qiang Kou qkou@umail.iu.edu School of Informatics and Computing, Indiana University [[alternative HTML version deleted]]
You can do that like below: startingDir<-"/myDirectory" filez<-list.files(startingDir) sapply(filez,FUN=function(eachPath){ file.rename(from=eachPath,to=sub(pattern="xxx",replacement="newTextString",eachPath)) }) On Mon, Feb 3, 2014 at 10:48 AM, arun <smartpink111@yahoo.com> wrote:> Hi, > Check ?file.rename() > http://stackoverflow.com/questions/10758965/how-do-i-rename-files-using-r > > > A.K. > > > I have list of files in folder > > e.g p126r62_5t19880613_nn1.tif.gz > p126r62_5t19880613_nn2.tif.gz > p200r65_5t19880613_nn1.tif.gz > > and I would like to change file name to > LT512606219880613XX_B1.tif.gz > LT512606219880613XX_B2.tif.gz > LT520006519880613XX_B1.tif.gz > > Does any body knows how It can be changed? > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Qiang Kou qkou@umail.iu.edu School of Informatics and Computing, Indiana University [[alternative HTML version deleted]]
Hi, Try: from1 <- list.files() ##folder where files are stored ?from1 #[1] "p126r62_5t19880613_nn1.tif.gz" "p126r62_5t19880613_nn2.tif.gz" #[3] "p200r65_5t19880613_nn1.tif.gz" to1 <- gsub("^p(\\d+)r(\\d+)\\_.{2}(\\d{8})\\_nn(.*)","LT5\\10\\2\\3XXXX_B\\4",from1) ?file.rename(from1,to1) #[1] TRUE TRUE TRUE ?list.files() #[1] "LT512606219880613XXXX_B1.tif.gz" "LT512606219880613XXXX_B2.tif.gz" #[3] "LT520006519880613XXXX_B1.tif.gz" A.K. Hi guys, Thank you for reply Actually I have thousands of ?files and I would like to change file name, then I can use it in my software ? e.g The original file ? ? ? p126r62_5t19880613_nn1.tif.gz The expected change LT512606219880613XXXX_B1.tif.gz The every single character in original files has some meaning ? p126r62_5t19880613_nn1.tif.gz p(path)126(path number) r (raw)62(rownumber)_5t(name of instrument)19880613(date)_nn1(band number).tif.gz So I would like to replace some part p ? ? ? = LT5 r ? ? ? ?= 0 _5t ? ?=remove _nn ? ?=B XXXX=add after date I have tried file.rename() function but I was unable to apply this change to all files. is it possible to use file.rename function to apply for all file? On Monday, February 3, 2014 10:48 AM, arun <smartpink111 at yahoo.com> wrote: Hi, Check ?file.rename() http://stackoverflow.com/questions/10758965/how-do-i-rename-files-using-r A.K. I have list of files in folder e.g p126r62_5t19880613_nn1.tif.gz ? ? ?p126r62_5t19880613_nn2.tif.gz ? ? ?p200r65_5t19880613_nn1.tif.gz and I would like to change file name to ? ? ?LT512606219880613XX_B1.tif.gz ? ? ?LT512606219880613XX_B2.tif.gz ? ? ?LT520006519880613XX_B1.tif.gz Does any body knows how It can be changed?