ID EN
I/O & Files

file.access

R Base 3.6.2

Utility function to access information about files on the user's file systems.

Syntax

R
file.access(names, mode = 0)

Arguments

Parameter Description
names character vector containing file names. Tilde-expansion will be done: see path.expand.
mode integer specifying access mode required: see ‘Details’.

Return Value

An integer vector with values 0 for success and -1 for failure.

Details

The mode value can be the exclusive or of the following values 0test for existence. 1test for execute permission. 2test for write permission. 4test for read permission. Permission will be computed for real user ID and real group ID (rather than the effective IDs). This function uses the C function _access in msvcrt.dll, but was written using Win32 API functions. Windows does not have the concept of an ‘executable file’, so this function regards directories and files with extension .exe, .bat, .cmd and .com as executable. (system and Sys.which make the same assumption.) UTF-8-encoded file names not valid in the current locale can be used. Please note that it is not a good idea to use this function to test before trying to open a file. On a multi-tasking system, it is possible that the acces

Examples

Example
R
# NOT RUN {
fa <- file.access(dir("."))
table(fa) # count successes & failures
d <- dir(file.path(R.home(), "bin"))
df <- dir(file.path(R.home(), "bin"), full.names = TRUE)
d[file.access(df, 0) == 0] # all exist
d[file.access(df, 1) == 0] # some are executable, some are not
d[file.access(df, 4) == 0] # hopefully all are readable
d[file.access(df, 2) == 0] # they may or may not be writable
# }

See Also

file.info for more details on permissions Sys.chmod to change permissions and try for a ‘test it and see’ approach. file_test for shell-style file tests.