[R] Change file name

arun smartpink111 at yahoo.com
Tue Feb 4 17:58:46 CET 2014


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?




More information about the R-help mailing list