Reputation: 1000
I'm trying to use fs::dir_ls()
to return the same results as the list.files()
example below. Ultimately, I'm just trying to return files that start with a specific pattern.
path <- "./path/to/files"
pattern <- "^ABC_.*\\.csv$"
# list files returns the expected output
list.files(path = path, pattern = pattern, full.names = T)
# [1] "path/to/files/ABC_1312.csv"
# [2] "path/to/files/ABC_ACAB.csv"
# dir_ls does not return any matching files
fs::dir_ls(path = path, regexp = pattern)
# character(0)
I think the issue here is that the scope of each method's pattern argument differs. The list.files()
pattern is only applied to the basename()
of the file path, whereas, the dir_ls()
regexp argument is being applied to the full path. As a result, the ^
regex is being applied to the start of the path, instead of the beginning of each file. Is there a way to limit the scope of dir_ls()
to only match patterns on the basename()
of each file similar to list.files()
? Any other insights are appreciated!
Upvotes: 2
Views: 1387
Reputation: 18631
See this issue on GitHub:
you need to modify your regular expression to match the full path then, or use a filtering function that only looks at the basename.
Use
pattern <- paste0(.Platform$file.sep, "ABC_.*\\.csv$)
You can also do something like
regexp = fs::path(path, pattern)
Upvotes: 1