[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  file.access: Ascertain File Accessibility

##  Description

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

##  Usage

    
    
    file.access(names, mode = 0)

##  Arguments

names

character vector containing file names. Tilde-expansion will be done: see ` [
path.expand ](/link/path.expand?package=base&version=3.6.2) ` .

mode

integer specifying access mode required: see ‘Details’.

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

0

    

test for existence.

1

    

test for execute permission.

2

    

test for write permission.

4

    

test 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
](/link/system?package=base&version=3.6.2) ` and ` [ Sys.which
](/link/Sys.which?package=base&version=3.6.2) ` 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
accessibility of a file will change between the time you call ` file.access()
` and the time you try to open the file. It is better to wrap file open
attempts in ` [ try ](/link/try?package=base&version=3.6.2) ` .

##  See Also

` [ file.info ](/link/file.info?package=base&version=3.6.2) ` for more details
on permissions, ` [ Sys.chmod ](/link/Sys.chmod?package=base&version=3.6.2) `
to change permissions, and ` [ try ](/link/try?package=base&version=3.6.2) `
for a ‘test it and see’ approach.

` [ file_test ](/link/file_test?package=base&version=3.6.2) ` for shell-style
file tests.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ffile.access&utm_source=r-docs&utm_medium=docs&utm_term=file.access&utm_content=run_example_in_workspace)

    
    
    # 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
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ffile.access&utm_source=r-docs&utm_medium=docs&utm_term=file.access&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)