Reputation: 1212
I have a directory with thousands of sub-directories.
Each subdirectory begins with a URL name, for example: /australia.gov.au_about-australia
I want to get a list of all the sub-directories that begin with a certain string, e.g. "australia.gov.au".
It appears that the list.dirs
function does not allow for pattern matching?
I have tried the following, to no avail:
testSite <- "australia.gov.au"
list.files(paste0("main-directory/",paste0("^[",testSite,"]")),
full.names = TRUE, recursive=TRUE, ignore.case = TRUE)`
Upvotes: 1
Views: 552
Reputation: 226162
Following @MrFlick's answer for the first part, but simplifying the rest slightly:
re <- paste0("^", gsub(".", "\\.", testSite, fixed=TRUE))
grep(re,list.dirs(),value=TRUE)
Upvotes: 1
Reputation: 206197
You can use Filter
to filter your directory list
testSite <- "australia.gov.au"
Filter(function(x) grepl(paste0("^", gsub(".", "\\.", testSite, fixed=TRUE)), x),
list.dirs())
We do some extra work to convert your URL to a regular expression to do the matching.
Upvotes: 3