[R] list.files recursively to find files in a specific way...

Keith Jewell k.jewell at campden.co.uk
Wed Jul 20 12:30:17 CEST 2011


I don't think the OP specified an operating system, but...

A few weeks ago I had a closely analogous problem, seeking files 'menus.txt' 
in subdirectories 'etc' but not from other subdirectories; 
'/etc/menus.txt'. I made this post 
<http://tolstoy.newcastle.edu.au/R/e14/help/11/06/5685.html>, but it was 
unanswered - I tend to ramble :-(

An appropriate Sys.glob construction was:
      Sys.glob(file.path(.libPaths(), "*/etc/menus.txt"))

I suggested that under MS Windows Sys.glob() cannot handle a UNC path 
beginning with backslashes.
Was I correct?

If so, I suggested an equivalent list.files construction (where I did escape 
'.') as:
  list.files(path=file.path(list.files(path=.libPaths(), full.names=TRUE), 
"etc"), pattern="^menus\\.txt$", full.names=TRUE)
Does that look OK?

Best regards,

Keith Jewell

"Prof Brian Ripley" <ripley at stats.ox.ac.uk> wrote in message 
news:alpine.LFD.2.02.1107200711180.30061 at gannet.stats.ox.ac.uk...
> But using the approproate tool, Sys.glob, whould be much simpler.
>
> Note that 'pattern' in list.files is
>
> - a regexp, and '.' is a special character in a regexp: Phil's solution 
> also needs to escape it or use fixed = TRUE
> - it is documented to match file *names*, not file paths.
>
> One of the authors of list.files and the author of Sys.glob
>
> On Tue, 19 Jul 2011, Phil Spector wrote:
>
>> Pei -
>>   A file pattern can't contain a directory separator, but it's easy to 
>> search for one outside the context of list.files.  I think
>>
>> grep('B/file2.txt',list.files(path = routeStr, all.files = TRUE,
>>                              full.names = TRUE, recursive = 
>> TRUE),value=TRUE)
>>
>> should give you what you want.
>>
>> - Phil Spector
>> Statistical Computing Facility
>> Department of Statistics
>> UC Berkeley
>> spector at stat.berkeley.edu
>>
>>
>> On Tue, 19 Jul 2011, JIA Pei wrote:
>>
>>> Hi, all:
>>>
>>> My folders are organized in such a way:
>>>
>>>
>>> root
>>>
>>> ----branch1
>>> ---------------A
>>> -----------------------file1.txt
>>> -----------------------file2.txt
>>> ---------------B
>>> -----------------------file1.txt
>>> -----------------------file2.txt
>>>
>>> ----branch2
>>> ---------------A
>>> -----------------------file1.txt
>>> -----------------------file2.txt
>>> ---------------B
>>> -----------------------file1.txt
>>> -----------------------file2.txt
>>>
>>> ...
>>>
>>> ----branch100
>>> ---------------A
>>> -----------------------file1.txt
>>> -----------------------file2.txt
>>> ---------------B
>>> -----------------------file1.txt
>>> -----------------------file2.txt
>>>
>>>
>>>
>>> I'd love to list all file2.txt from all subdirectories "B"s but not from
>>> "A"s, how to do that?
>>>
>>> I tried the following two
>>>
>>> a) allResults <- list.files(path = routeStr, pattern = "file2.txt",
>>> all.files = TRUE, full.names = TRUE, recursive = TRUE);
>>> gives me 200 files in allResults, which is wrong. There should be only 
>>> 100
>>> files in allResults.
>>>
>>> b) allResults <- list.files(path = routeStr, pattern = "B/file2.txt",
>>> all.files = TRUE, full.names = TRUE, recursive = TRUE);
>>> still wrong. It give me nothing, namely, 0 file(s) in allResults.
>>>
>>>
>>> Can anybody help to solve this problem?
>>>
>>>
>>> Best Regards
>>> Pei
>>>
>>> -- 
>>>
>>> Pei JIA
>>>
>>> Email: jp4work at gmail.com
>>> cell:    +1 604-362-5816
>>>
>>> Welcome to Vision Open
>>> http://www.visionopen.com
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide 
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>



More information about the R-help mailing list