Skip to contents

Check that only one file in a directory has a name matching pattern, for example before attempting to read a file.

Usage

get_file_path(dir = ".", pattern, ignore_case = TRUE, quietly = FALSE)

Arguments

dir

character string containing the path to a directory.

pattern

character string containing a regular expression used to select names of files that are present in dir.

ignore_case

TRUE or FALSE: use case-insensitive pattern matching?

quietly

TRUE or FALSE: suppress the message with the found file name?

Value

A character string with the absolute normalized path to the file with a name matching pattern if there is exactly one such file in directory dir. Otherwise an error is thrown. Use basename() on the result to obtain the filename itself.

Details

The default dir (".") indicates the working directory.

If ignore_case is FALSE and no case-sensitive match is found, the error message indicates if any case-insensitive match is present.

In contrast to the default of list.files(), get_file_path() also finds 'hidden' files, i.e., files with names that start with a dot, and excludes directories.

Paths will be normalized to ensure they still work if the working directory changes.

See also

checkinput::is_path() to check if a path is valid, with a Note on paths and extensive references about file paths and directories; create_file_path() to create a file path and creating the indicated directory if it does not yet exist; fs::file_exists() and list.files() (which includes directories) to check for existence of files without checking they are a unique match to a pattern; file.info() and file.access() to extract information about files or directories

Other functions to handle paths and directories: create_dir(), create_file_path(), create_tempdir()

Other functions to check equality: are_equal(), check_case(), not_in(), replace_vals()

Examples

# Create files in a temporary directory so we know what is present.
my_tempdir <- create_tempdir(prefix = "examplegetfilepath")
my_tempfiles <- fs::path_abs(
  fs::path(my_tempdir, paste0(c("some_filename", "another_filename"), ".txt"))
)

# Create the files
file.create(my_tempfiles)
#> [1] TRUE TRUE

get_file_path(dir = my_tempdir, pattern = "some_file")
#> Using file '/tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0/some_filename.txt'
#> /tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0/some_filename.txt

# The same file is found if case-insensitive matching is used:
get_file_path(dir = my_tempdir, pattern = "SOME_FILE", ignore_case = TRUE)
#> Using file '/tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0/some_filename.txt'
#> /tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0/some_filename.txt

# Error reporting the presence of a case-insensitive match.
try(get_file_path(dir = my_tempdir, pattern = "SOME_FILE", ignore_case = FALSE))
#> Error in get_file_path(dir = my_tempdir, pattern = "SOME_FILE", ignore_case = FALSE) : 
#>   No case-sensitive matches to pattern 'SOME_FILE' are present in directory
#> '/tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0'.
#> However, a case-insensitive match to 'pattern' is present: 'some_filename.txt'.

# 'pattern' is interpreted as a regular expression
get_file_path(dir = my_tempdir, pattern = "^.+er_file")
#> Using file '/tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0/another_filename.txt'
#> /tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0/another_filename.txt

# Error reporting no match found.
try(get_file_path(dir = my_tempdir, pattern = "missing_filename_abcde",
                 ignore_case = TRUE))
#> Error in get_file_path(dir = my_tempdir, pattern = "missing_filename_abcde",  : 
#>   No matches to pattern 'missing_filename_abcde' are present in directory
#> '/tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0'.
try(get_file_path(dir = my_tempdir, pattern = "missing_filename_abcde",
                 ignore_case = FALSE))
#> Error in get_file_path(dir = my_tempdir, pattern = "missing_filename_abcde",  : 
#>   No case-sensitive matches to pattern 'missing_filename_abcde' are present in directory
#> '/tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0'.
#> No case-insensitive match is present either.

# Error if multiple matches are present.
try(get_file_path(dir = my_tempdir, pattern = "_filename"))
#> Error in get_file_path(dir = my_tempdir, pattern = "_filename") : 
#>   Multiple matches to pattern '_filename' are present in directory
#> '/tmp/Rtmp31i27G/examplegetfilepath1a623d86c3e0': 'another_filename.txt', 'some_filename.txt'!

# Clean up
unlink(x = my_tempfiles)
rm(my_tempfiles)