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

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jul 20 08:15:06 CEST 2011


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