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
  • 36,904
  • 9
  • 50
  • 61
Chinteck
  • 3
  • 2

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
  • 19,848
  • 8
  • 66
  • 97
  • 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 – Chinteck Nov 17 '15 at 13:39