R: Wildcard Expansion on File Paths

Sys.glob {base} | R Documentation

## Wildcard Expansion on File Paths

### Description

Function to do wildcard expansion (also known as ‘globbing’) on file paths.

### Usage

    Sys.glob(paths, dirmark = FALSE)

### Arguments

`paths` | character vector of patterns for relative or absolute filepaths. Missing values will be ignored.
`dirmark` | logical: should matches to directories from patterns that do not already end in `/` or `\` have a slash appended? May not be supported on all platforms.

### Details

The `glob` system call is not part of Windows, and we supply a partial emulation.

Wildcards are `*` (match zero or more characters) and `?` (match a single character). If a filename starts with `.` this must be matched explicitly (on Windows, but note that this is platform-dependent).

`[` begins a character class. If the first character in `[...]` is not `!`, this is a character class which matches a single character against any of the characters specified. The class cannot be empty, so `]` can be included provided it is first. If the first character is `!`, the character class matches a single character which is none of the specified characters. Whether `.` in a character class matches a leading `.` in the filename is OS-dependent.

Character classes can include ranges such as `[A-Z]`: include `-` as a character by having it first or last in a class. (In the current implementation ranges are in the numeric order of Unicode code points.)

One can remove the special meaning of `?`, `*` and `[` by preceding them by a backslash (except within a character class). Note that on Windows `?` and `*` are not valid in file names, so this is mainly for consistency with other platforms.

File paths in Windows are interpreted with separator `\` or `/`. Paths with a drive but relative (such as `c:foo\bar`) are tricky, but an attempt is made to handle them correctly. An attempt is made to handle UNC paths starting with a double backslash. UTF-8-encoded paths not valid in the current locale can be used.

### Value

A character vector of matched file paths. The order is system-specific (but in the order of the elements of `paths`): it is normally collated in either the current locale or in byte (ASCII) order; however, on Windows collation is in the order of Unicode points.

Directory errors are normally ignored, so the matches are to accessible file paths (but not necessarily accessible files).

### See Also

`path.expand`.

Quotes for handling backslashes in character strings.

### Examples

    Sys.glob(file.path(R.home(), "library", "*", "R", "*.rdx"))

[Package base version 3.5.1 Index]