Search code examples
rexport-to-csvread.csv

Get name of file in read.csv same as in write.csv


I would like to achieve that the file I write.csv has the same name as the file I use with read.csv2. See below: I am reading a file name "2005Calc.csv" and want to write it as: "2005Calc28-01-2021.csv". How can I do this? I tried something below and it works..only I am not able to add the csv file name to it.

Thank you in advance.

    LKCalc <- read.csv2("2005Calc.csv")

    date<-format(Sys.time(), "%d-%m-%Y")

    csvFileName <- paste("FILENAME",date,".csv",sep="")

    write.csv(LKCalc, file=csvFileName) 

Solution

  • "FILENAME" is a literal string, not a variable referring to a file name. How about (untested):

    FILENAME <- "2005Calc"
    LKCalc <- read.csv2(paste0(FILENAME,".csv"))
    date <- format(Sys.time(), "%d-%m-%Y")
    csvFileName <- paste0(FILENAME,date,".csv")
    write.csv(LKCalc, file=csvFileName)