Add file extension to all files in a folder in R

送分小仙女□ 提交于 2019-12-12 10:57:36

问题


As a result from a loop I have a lot of files without a file extension in a folder.

How could I add a file extension (.png) to all files in the folder while keeping the original name, e.g. from NAME1 to NAME1.png, NAME3 to NAME3.png, NAME6 to NAME6.png etc using R?


回答1:


With the list.files function you can retrieve the file names under a given path and with a given pattern. From them you can use paste to add your file extension and next file.rename to rename the files. For instance:

    oldNames<-list.files(...) #some argument here
    newNames<-paste(sep="",oldNames,".png")
    for (i in 1:length(oldNames)) file.rename(oldNames[i],newNames[i])



回答2:


Install the pathological package and use replace_extension.

library(devtools)
install_github("pathological", "richierocks")

library(pathological)
old_filenames <- paste0("NAME", 1:6)
new_filenames <- replace_extension(, "png")
file.rename(old_filenames, new_filenames)


来源:https://stackoverflow.com/questions/25741878/add-file-extension-to-all-files-in-a-folder-in-r

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