rename multiple files in a folder using R [duplicate]

为君一笑 提交于 2019-12-30 11:54:27

问题


I have a folder which contains several files named by the date the data was measured. For example: "07182014.csv","07192014.csv"...

Since I have multiple stations for the measurements, I would like to add the station number before each file name for distinguish purposes. For example, the file "07182014.csv" will become "N1_07182014.csv".

I'm new to R, and most of the time I search the web for solutions to my data analysis problem.

Can someone help me revise the code I'm having below so I can properly rename all files in the folder? Or if any other better solution can be provided, that will be helpful!

setwd("C:\\data")
files <- list.files() 
sapply(files,FUN=function(eachPath){ 
file.rename(from=eachPath,to=sub(pattern="[$.csv]", paste0("N0_"),eachPath)) 
}) 

Thanks a lot!


回答1:


Here is my answer:

folder = "C:\\data"
files <- list.files(folder,pattern = "*.CSV",full.names = T) 
   sapply(files,FUN=function(eachPath){ 
   file.rename(from=eachPath,to= sub(pattern="\\/", paste0("\\/N0_"),eachPath))
 })


来源:https://stackoverflow.com/questions/25673643/rename-multiple-files-in-a-folder-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!