Check for file existence, readability, or writability

DESCRIPTION:

Determines whether the files specified in names have the access modes specified by mode, for example, whether the files exist or are writable.

USAGE:

access(names, mode=0) 
file.access(names, mode=0)

REQUIRED ARGUMENTS:

names
vector of character strings containing legal file names.

OPTIONAL ARGUMENTS:

mode
numeric vector specifying access modes. If this vector is shorter than the names vector, it is repeated cyclically. The values for the elements of mode are as follows:

0 - file existence

1 - execute permission

2 - write permission

4 - read permission

and they can be added together, i.e., a mode of 5 is equivalent to asking for both read and execute permission. (On DOS, every file that exists has both read and execute permissions, so the typical value for mode is a vector of 0s and 2s.)

VALUE:

access returns a numeric vector of length length(names) consisting of 0s (if the file has the specified access) and -1s (if the file does not).

The value of file.access is a named vector: the result of calling access then using the value of the names argument as the vector's names attribute.

DETAILS:

The access function is an implementation of the C library function access.

The file.access function emulates R's function of the same name.

SEE ALSO:

, .

EXAMPLES:

access("trytoed.it", 2) 
# a return value of -1 indicates the file is not writable (it may or  
# may not exist) 
access("trytoed.it", 0) 
# a subsequent return value of 0 indicates that the file exists  
# (implying the file is read-only) 

# check access mode of two files (one does not exist)
tfile1 <- tempfile("a")
tfile2 <- tempfile("b")
cat("Today's date is:",date(),"\n", file=tfile1)
file.access(c(tfile1,tfile2),mode=4)
unlink(tfile1)