简体   繁体   中英

Get files number in a dir in R?

in shell ,to make a dir:

mkdir  /home/test

then ,to create a file named ".test" in the "/home/test"

a=list.files(path = "/home/test",include.dirs = FALSE)
a
character(0)
a=list.files(path = "/home/test",include.dirs = TRUE)
a
character(0)
a=list.files(path = "/home/test/",include.dirs = TRUE)
a
character(0)
list.files(path = '/home/test', all.files=TRUE,inclued.dirs=FALSE)
[1] "."     ".."    ".test"
a=list.files(path = '/home/test', all.files=TRUE)
length(a)
[1] 3

how can i get length(a) = 1 using regular expression parameters pattern= in list.files to prune . and ..

Use all.files=TRUE to show all file names including hidden files.

list.files(path = '/home/test', all.files=TRUE)

To answer your edit, one way would be to use a negative number with tail

tail(list.files(path = '/home/test', all.files=TRUE), -2)

Using only the pattern argument:

list.files(path='/home/test', all.files=TRUE, pattern="^[^\\.]|\\.[^\\.]")

The pattern says "anything that starts with something other than a dot or anything that starts with a dot followed by anything other than a dot."


Although it breaks your requirement to use the pattern argument of list.files , I would actually probably wrap grep around list.statements in this case.

grep("^\\.*\\.$", list.files(path='/home/test', all.files=TRUE), 
     invert=TRUE, value=TRUE)

The above will find any file names that only contain dots, then return everything else. invert=TRUE means "find the names that do not match", and value=TRUE means "return the names instead of their location."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM