isDirPath: Test paths to see if they are for a directory

View source: R/fileUtils.R

isDirPathR Documentation

Test paths to see if they are for a directory

Description

If a path ends in "/" or exists and is a directory, it is a directory path. To do a pure lexical check, which just looks for the ending "/" and ignores the file system, set lexical= TRUE. Note that paths ending in "/" are directory paths even when they don't exist and even if they could not be created due to illegal characters or the existence of a file with that name (sans the terminal "/"). e.g. some/path as a file does not affect ⁠some/path/⁠ being reported as a directory.

Usage

isDirPath(paths, lexical = FALSE)

Arguments

paths

Vector of paths to check and see if they are directories.

lexical

Set TRUE to skip secondary check of the file system for paths that don't end in "/" but might already exist as directories.

Details

Any leading "~" is expanded a la path.expand(), but Sys.glob() is not used, e.g. "*" is just a regular character.

Value

Returns a logical vector, TRUE for paths to treat as directories, FALSE otherwise.

See Also

  • dir.exists(): to just check if a directory exists.

  • dirname(): to extract the directory part of a file path (ignores end "/").

  • Sys.glob(): to expand glob patterns into a vector of paths (including optionally adding a terminal "/" to directories).

  • path.expand(): to expand leading "~" to home directory, where supported.

Examples


# By default, only false if not ending in "/" and not an existing directory
isDirPath( c("./", ".", "noSuchDir/", "noSuchDir", "~", "*" ))
#> [1] TRUE, TRUE, TRUE, FALSE, TRUE, FALSE

# With lexical=TRUE, only cares about ending "/"
isDirPath( c( "./", ".", "noSuchDir/", "noSuchDir", "~", "*" ),
           lexical= TRUE )
#> [1] TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE


jefferys/JefferysRUtils documentation built on Jan. 12, 2024, 9:18 p.m.