I'm trying to extract a filename and save the dataframe with that same name. The problem I have is that if the filename for some reason is inside a folder with a similar word, stringr will return that word as well.
filename <- "~folder/testdata/2016/testdata 2016.csv"
If I run this:
library(stringr)
str <- str_trim(stringr::str_extract(filename,"[t](.*)"), "left")
it returns testdata/2016/testdata 2016.csv
when all I want is testdata 2016
. Optimally it would even be better to get testdata2016
.
I've been trying several combinations but there has to be a simpler way of doing this. If there was a way of reading the path from right to left, starting at .csv
stop at /
, I wouldn't have this issue.