get_filename: Extract Filenames from File Paths

View source: R/get_filename.R

get_filenameR Documentation

Extract Filenames from File Paths

Description

The get_filename function extracts filenames from file paths with options to remove file extensions and/or directory paths.

Usage

get_filename(paths, rm_extension = TRUE, rm_path = TRUE)

Arguments

paths

A character vector containing file system paths. Must be valid and accessible path strings.

rm_extension

A logical flag controlling file extension removal:

  • TRUE: Strips file extensions from filenames

  • FALSE: Preserves complete filename with extension Default is TRUE.

rm_path

A logical flag managing directory path handling:

  • TRUE: Extracts only the filename, discarding directory information

  • FALSE: Retains complete path information Default is TRUE.

Details

The function performs the following operations:

  • Validates input paths

  • Handles empty input vectors

  • Optionally removes directory paths using basename

  • Optionally removes file extensions using regex substitution

Value

A character vector of processed filenames with applied transformations.

Note

  • If both rm_extension and rm_path are FALSE, a warning is issued and the original paths are returned

  • Supports multiple file paths in the input vector

See Also

  • base::basename() for basic filename extraction

Examples

# Example: File path processing demonstrations

# Setup test files
xlsx_files <- mintyr_example(
  mintyr_examples("xlsx_test")    # Get example Excel files
)

# Example 1: Extract filenames without extensions
get_filename(
  xlsx_files,                     # Input file paths
  rm_extension = TRUE,            # Remove file extensions
  rm_path = TRUE                  # Remove directory paths
)

# Example 2: Keep file extensions
get_filename(
  xlsx_files,                     # Input file paths
  rm_extension = FALSE,           # Keep file extensions
  rm_path = TRUE                  # Remove directory paths
)

# Example 3: Keep full paths without extensions
get_filename(
  xlsx_files,                     # Input file paths
  rm_extension = TRUE,            # Remove file extensions
  rm_path = FALSE                 # Keep directory paths
)

mintyr documentation built on April 4, 2025, 2:56 a.m.