get file location in R, path to file in R

Get file location in R

Here are multiple scenarios on how to get file location in R. It can be a directory or a full path, and sometimes there are specific file types that you want to detect. A typical use case is when combining data from multiple files, for example, rds files or other formats.


Get file location in directories and subdirectories using R

With the function list.files you can get names of one or multiple files in the exact directory or its subdirectories. If you do not specify file type you will get all of them, including names of subdirectories.


#[1] "my.pdf"       "MyFile1.xlsx" "MyFile2.xlsx" "New folder"   "sample.csv"

If you want to get the names of all Excel files in a certain directory and including subdirectories you can do that like this.

  pattern = ".xlsx",
  recursive = TRUE

#[1] "MyFile1.xlsx"            "MyFile2.xlsx"            "New folder/My.File3.xlsx"

In the case of a couple of specific file types, you can do that similarly to multiple text pattern detection.

ptrn <- paste(".xlsx", ".csv", sep = "|")

  pattern = ptrn,
  recursive = TRUE

#[1] "MyFile1.xlsx"            "MyFile2.xlsx"            "New folder/My.File3.xlsx"
#[4] "sample.csv"

To get the full path to the necessary files, try to use the parameter full.names.

  pattern = ".xlsx",
  recursive = TRUE,
  full.names = TRUE

#[1] "C:/Users/datacornering/Downloads/MultiFiles/MyFile1.xlsx"
#[2] "C:/Users/datacornering/Downloads/MultiFiles/MyFile2.xlsx"
#[3] "C:/Users/datacornering/Downloads/MultiFiles/New folder/My.File3.xlsx"


Get only directory from the path in R

With the function dirname, you can get the path to the directory from the full path.


#[1] "C:/MyLocation"

Try to use the function list.dirs if it is possible. It will return exactly what is necessary.


#[1] "C:/Users/datacornering/Downloads/MultiFiles"           
#[2] "C:/Users/datacornering/Downloads/MultiFiles/New folder"


Get only the file name from the path in R

With the function basename, you can get only the file name from the path including the extension.


#[1] "MyExcel.xlsx"

If you want to remove the file extension in the file name, then here is a simple example of how to do that. When only one file type is involved, try to use the function sub.

sub(".xlsx", "", basename("C:/MyLocation/MyExcel.xlsx"))

#[1] "MyExcel"

A universal way to get only file names without the extension is with the additional use of regex or the function file_path_sans_ext.

x <- list.files(
  pattern = ".xlsx",
  recursive = TRUE

sub('(.*)\\..*$', '\\1', basename(x))

#[1] "MyFile1"  "MyFile2"  "My.File3"


#[1] "MyFile1" "MyFile2" "My.File3"


You might find this useful

Here is another example that is useful to get the current R script file location while running in interactive or batch mode.

If you are using the path to files that you can get from Windows, here is how to fix that for usage in R.

Besides the file path, you can also read when it is created or modified, and here is how to do that.




Leave a Reply

Your email address will not be published. Required fields are marked *