[R] Rename multiple files in a directory and write renamed files back to directory

Zilefac Elvis zilefacelvis at yahoo.com
Fri Dec 5 04:44:42 CET 2014


Hello,
I would like to rename multiple files in a directory. Filenames are read using:

lfile <- list.files(pattern="rcp45_Daily_") 
files <-  paste(paste(getwd(),lfile,sep="/"), list.files(lfile),sep="/")# getwd of these files 

dput(lfile) 
c("rcp45_Daily_Sim001.dat", "rcp45_Daily_Sim002.dat") 


- How can I rename these files (200 in number) using something like:
  file.rename(lfile, paste0("rcp45_Daily_Sim", 1:200))?The new filenames should be rcp45_Daily_Sim001, rcp45_Daily_Sim002, ..., rcp45_Daily_Sim200.

- I would like to write the new file names to the directory.

The data files contain huge amounts of data and should not be read into R. Only the file names should change.

Many thanks for your helpful answers.
Asong.



More information about the R-help mailing list