Import csv with changing date

Hi is there a way to import a csv file that gets exported to my downloads folder daily.
Basically I need to import the csv file daily and change some information on it and then export new csv file. Cani do this with out having to change the file name in the read.csv path?

Thankyou

You could create a folder inside Downloads, place your file in there and then use the following code snippet.

my_file <- list.files(path = "C:/Downloads/MyFolder", 
                      pattern = "*.csv", 
                      full.names = TRUE)

my_data <- read.csv(my_file)

If you have multiple files with, say, the same prefix but a different suffix each day (for example, "myfile_20200430.csv", "myfile_20200501.csv", etc.) you can identify and load the the most recent file in the sequence as follows:

# Function to identify the most recent file with a given name pattern
#  in a given folder
# mtime is the date when the file was last modified. You could change this 
#  to ctime to identify the latest file by the most recent creation date.
latest_file = function(fpattern, fpath) {
  f = list.files(pattern=fpattern, path=fpath, full.names=TRUE)
  f = file.info(f)
  rownames(f)[which.max(f$mtime)] 
}

myfile = latest_file(fpattern="myfile.*csv", fpath="~/downloads")
d = readr::read_csv(myfile)

Thanks for the help. I'm just having an issue with this now.

i can't tell what the problem is. Can you add some additional information about what's going wrong?

Thanks Joels, It worked great. Thankyou so much for the help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.