0

I would like to use R to verify system date first prior to go to my default work directory to open today and the day before data file.

The reason is my data file been labelled as 20151101_xxx.csv which is yyyymmdd_xxx.csv. Is it possible R can perform this action?

lmo
  • 37,904
  • 9
  • 56
  • 69
Chinteck
  • 3
  • 2
  • 2
    Yes. Hint: Look at `Sys.Date()`. – Heroka Nov 17 '15 at 12:46
  • Thanks for the hint. I try to use below comment: t1 <- as.character(Sys.Date()-1) t <-as.character(Sys.Date()) but not sure how to remove the "-". Reason change it to character is due to going to replace this into next comment – Chinteck Nov 17 '15 at 12:57
  • Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It makes it easier for others to help you. – Heroka Nov 17 '15 at 12:59
  • Is `paste0(format(Sys.Date()-1,"%Y%m%d"),"_xxx.csv")` what you wanted ? – etienne Nov 17 '15 at 13:06

1 Answers1

0

You can use list.files to do that as follows:

days <- gsub("-", "", Sys.Date() - 0:1)
list.files(path = "YOUR PATH", pattern = paste0(days, "_", collapse = "|"))

Which gives you all files starting with yyyymmdd_.

To only get files in the format yyyymmdd_xxx.csv do the following:

list.files(path = "YOUR PATH", pattern = paste0(days, "_xxx.csv", collapse = "|"))

This should give you all files containing yyyymmdd_xxx.csv.

Rentrop
  • 20,979
  • 10
  • 72
  • 100
  • A tip: you can use `Sys.Date()` in combination with format: `format(Sys.Date(), format="%Y%m%d")` – Heroka Nov 17 '15 at 13:12
  • Thanks. unfortunately i have multiple file 20151101_xxx.csv, 20151101_yyy.csv, 20151031_xxx.csv, 20151031_yyy, etc. However, I just would like to open file 20151101_xxx.csv and 20151031_xxx.csv. – Chinteck Nov 17 '15 at 13:36
  • i get what i want but edit the below comment: days <- gsub("-", "", Sys.Date() - 0:1) list.files(path = "YOUR PATH", pattern = paste0(days, "_xxx", collapse = "|")). thanks a lot – Chinteck Nov 17 '15 at 13:39