MCS
MCS

Reputation: 1101

Get all the paths that lead to a specific file

I need to access (and rbind) all the files in a directory that have the same name (eg. results.csv). Within the same directory files are organised according to different criteria, and it is possible that paths differ substantially (the hierarchy of folders might change).

How do I go about listing for each file name all the paths?

Upvotes: 0

Views: 64

Answers (1)

Sirius
Sirius

Reputation: 5429


all.the.files <- dir( "under/this/directory/", recursive=TRUE, pattern="^results\\.csv$" )

library(purrr)
all.the.data <- 
    all.the.files %>%  map_dfr( ~ read.csv(.x) )

Not sure if you are looking at a set of different filenames that you need to sum up, but replace the fixed filename in that code with a vairable if you like.

You said you wanted to read them all. As long as they have the same structure, it should be straight forward as proposed.

Upvotes: 1

Related Questions