Edjotace
Edjotace

Reputation: 132

Read latest txt in a Folder R

I am working with stock files from a repository, a new file is generated every day:

For example: "stock2021-11-05.txt"

I need to read the last generated file, or in its defect read all the files that begin with the word stock, join them.

I currently use the following code:

fileList <- list.files( pattern=  "*.txt")

But this brings me all the txt files from the repository and not just the ones that start with the word stock.

I would appreciate a help with this.

Thanks!

Upvotes: 0

Views: 63

Answers (2)

Philip Schalk
Philip Schalk

Reputation: 61

So you managed to filter out files which are not .txt files. Two steps are missing. A possible fileList now could be:

fileList <- c("stock2021-11-05.txt",
              "stock2020-11-15.txt",
              "stock2021-02-05.txt",
              "vwxyz2018-01-01.txt")

1 - Filter stock-files

> fileList_stock <- grep("^stock", fileList, value = TRUE)
> fileList_stock
[1] "stock2021-11-05.txt" "stock2020-11-15.txt" "stock2021-02-05.txt"

2 - Get latest file

> sort(fileList_stock, decreasing = TRUE)[1]
[1] "stock2021-11-05.txt"

1+2 - Wrapper function

> get_last_stock_file <- function(x){
+   grep("^stock", x, value = TRUE) %>% sort(decreasing = TRUE) %>% .[1]
+ }
> get_last_stock_file(fileList)
[1] "stock2021-11-05.txt"

Upvotes: 0

Henry Holm
Henry Holm

Reputation: 545

Simply use:

list.files(pattern = "stock.*\\.txt")

to find all files that begin with "stock" and end with ".txt"

Check out this REGEX cheat sheet from the stringr package to learn more: https://github.com/rstudio/cheatsheets/blob/main/strings.pdf

Upvotes: 1

Related Questions