Hello, when I use list.files with recursive = TRUE and all.files = TRUE, R returns a list of strings/paths.>From all those strings I want to keep only the ones starting with a .I tried using grep to achieve that. However, the problem is that because of the recursive list.files parameter, for some files beginning with a . there is a path attached. I think it is not as simple as it looks because all files end with . something. .xlsx or .txt or .r... files <- list.files("~", recursive = TRUE, all.files = TRUE) files /XXX/ZZZ/.R_history /XXX/ZZZ/AAA/Script.r /XXX/ZZZ/BBB/test.xlsx /XXX/ZZZ/CCC/.xyz files <- files[grep("^.*$", files)] I want grep to return only the following to lines: /XXX/ZZZ/.R_history /XXX/ZZZ/CCC/.xyz Any ideas on how to solve that issue? Cheers syrvn -- View this message in context: http://r.789695.n4.nabble.com/list-files-find-files-beginning-with-a-tp4635773.html Sent from the R help mailing list archive at Nabble.com.
On 08.07.2012 14:47, syrvn wrote:> Hello, > > when I use list.files with recursive = TRUE and all.files = TRUE, R returns > a list of strings/paths. >>From all those strings I want to keep only the ones starting with a . > I tried using grep to achieve that. However, the problem is that because of > the recursive list.files parameter, > for some files beginning with a . there is a path attached. I think it is > not as simple as it looks because all files > end with . something. .xlsx or .txt or .r... > > files <- list.files("~", recursive = TRUE, all.files = TRUE) > files > /XXX/ZZZ/.R_history > /XXX/ZZZ/AAA/Script.r > /XXX/ZZZ/BBB/test.xlsx > /XXX/ZZZ/CCC/.xyz > > files <- files[grep("^.*$", files)] > > I want grep to return only the following to lines: > > /XXX/ZZZ/.R_history > /XXX/ZZZ/CCC/.xyzYou do not need grep, let list.files do all the stuff for you: list.files("~", recursive = TRUE, all.files = TRUE, pattern="^\\.") Best, Uwe Ligges> Any ideas on how to solve that issue? > > Cheers > syrvn > > -- > View this message in context: http://r.789695.n4.nabble.com/list-files-find-files-beginning-with-a-tp4635773.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
On 08/07/2012 14:35, Uwe Ligges wrote:> > > On 08.07.2012 14:47, syrvn wrote: >> Hello, >> >> when I use list.files with recursive = TRUE and all.files = TRUE, R >> returns >> a list of strings/paths. >>> From all those strings I want to keep only the ones starting with a . >> I tried using grep to achieve that. However, the problem is that >> because of >> the recursive list.files parameter, >> for some files beginning with a . there is a path attached. I think it is >> not as simple as it looks because all files >> end with . something. .xlsx or .txt or .r... >> >> files <- list.files("~", recursive = TRUE, all.files = TRUE) >> files >> /XXX/ZZZ/.R_history >> /XXX/ZZZ/AAA/Script.r >> /XXX/ZZZ/BBB/test.xlsx >> /XXX/ZZZ/CCC/.xyz >> >> files <- files[grep("^.*$", files)] >> >> I want grep to return only the following to lines: >> >> /XXX/ZZZ/.R_history >> /XXX/ZZZ/CCC/.xyz > > > You do not need grep, let list.files do all the stuff for you: > > list.files("~", recursive = TRUE, all.files = TRUE, pattern="^\\.")Some people find the equivalent pattern = "^[.]" clearer to read. -- 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
It's all documented in ?regexp. "." is a regex metacharacter. Metacharacters have to be escaped via "\\". In particular, note: "The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any metacharacter with special meaning may be quoted by preceding it with a backslash. The metacharacters in EREs are . \ | ( ) [ { ^ $ * + ?, but note that whether these have a special meaning depends on the context. " and "Patterns are described here as they would be printed by cat: (*do remember that backslashes need to be doubled when entering R character strings*, e.g. from the keyboard). " So presumably files <- files[grep("^\\.*$", files)] is what you want. -- Bert On Sun, Jul 8, 2012 at 5:47 AM, syrvn <mentor_@gmx.net> wrote:> Hello, > > when I use list.files with recursive = TRUE and all.files = TRUE, R returns > a list of strings/paths. > >From all those strings I want to keep only the ones starting with a . > I tried using grep to achieve that. However, the problem is that because of > the recursive list.files parameter, > for some files beginning with a . there is a path attached. I think it is > not as simple as it looks because all files > end with . something. .xlsx or .txt or .r... > > files <- list.files("~", recursive = TRUE, all.files = TRUE) > files > /XXX/ZZZ/.R_history > /XXX/ZZZ/AAA/Script.r > /XXX/ZZZ/BBB/test.xlsx > /XXX/ZZZ/CCC/.xyz > > files <- files[grep("^.*$", files)] > > I want grep to return only the following to lines: > > /XXX/ZZZ/.R_history > /XXX/ZZZ/CCC/.xyz > > Any ideas on how to solve that issue? > > Cheers > syrvn > > -- > View this message in context: > http://r.789695.n4.nabble.com/list-files-find-files-beginning-with-a-tp4635773.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]