Skip to content
Related Articles

Related Articles

Improve Article

Read all Files in Directory using R

  • Difficulty Level : Expert
  • Last Updated : 06 Jun, 2021

To list all files in a directory in R programming language we use list.files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories. If no files are present in the directory, it returns “”. If a path does not exist it is skipped and a warning is shown. 

Note: File naming conventions depend on the platform.

Syntax:

list.files(path, pattern, all.files, full.names)

Parameter:



Path:  It is a character vector that contains pathname to directories.

Pattern:  It is an optional regular expression. If this argument is provided then the function only returns the files that have given pattern in their name.

all.files: It is a logical value. 

  • FALSE: Only the names of visible files are returned.
  • TRUE:  All file names will be returned irrespective of whether they are visible or hidden.

full.names:  It is a logical value.

  • TRUE: The filename also contains the full path to the directory.
  • FALSE: The filename doesn’t contain the path to the directory.

Directory in use:

Directory used

Example 1:

R




list.files(path=".", pattern=NULL, all.files=FALSE,
    full.names=FALSE)

Output:

Output

It is also possible to produce only some specific files for that the extension of those files is passed as the value to parameter pattern.

Example 2:

R




list.files(path=".", pattern=".pdf", all.files=TRUE,
    full.names=TRUE)

Output:

Output

My Personal Notes arrow_drop_up
Recommended Articles
Page :