Add file extension to all files in R folder

As a result, out of the loop, I have many files without file extension in the folder.

How can I add a file extension (.png) to all files in a folder while keeping the original name eg. from NAME1

to NAME1.png

, NAME3

to NAME3.png

, NAME6

to NAME6.png

, etc. using R?

+3


source to share


2 answers


With the help of the function, list.files

you can get the names of files at a given path and with a given pattern. Of these, you can use paste

to add the file extension and the next file.rename

one to rename the files. For example:



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

      

+4


source


Install the package pathological

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)

      

+1


source







All Articles