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

Phil Spector spector at stat.berkeley.edu
Wed Jul 20 00:56:59 CEST 2011


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.
>



More information about the R-help mailing list