utils_file: Utilities for file manipulation

utils_fileR Documentation

Utilities for file manipulation

Description

  • file_extension() Get the extension of a file.

  • file_name() Get the name of a file.

  • file_dir() Get or directory of a file

  • manipulate_files() Manipulate files in a directory with options to rename (insert prefix or suffix) and save the new files to the same or other provided directory.

  • pliman_indexes() Get the indexes available in pliman.

  • pliman_indexes_eq() Get the equation of the indexes available in pliman.

Usage

file_extension(file)

file_name(file)

file_dir(file)

manipulate_files(
  pattern,
  dir = NULL,
  prefix = NULL,
  name = NULL,
  suffix = NULL,
  extension = NULL,
  sep = "",
  save_to = NULL,
  overwrite = FALSE,
  remove_original = FALSE,
  verbose = TRUE
)

pliman_indexes()

pliman_indexes_eq()

Arguments

file

The file name.

pattern

A file name pattern.

dir

The working directory containing the files to be manipulated. Defaults to the current working directory.

prefix, suffix

A prefix or suffix to be added in the new file names. Defaults to NULL (no prefix or suffix).

name

The name of the new files. Defaults to NULL (original names). name can be either a single value or a character vector of the same length as the number of files manipulated. If one value is informed, a sequential vector of names will be created as "name_1", "name_2", and so on.

extension

The new extension of the file. If not declared (default), the original extensions will be used.

sep

An optional separator. Defaults to "".

save_to

The directory to save the new files. Defaults to the current working directory. If the file name of a file is not changed, nothing will occur. If save_to refers to a subfolder in the current working directory, the files will be saved to the given folder. In case of the folder doesn't exist, it will be created. By default, the files will not be overwritten. Set overwrite = TRUE to overwrite the files.

overwrite

Overwrite the files? Defaults to FALSE.

remove_original

Remove original files after manipulation? defaults to FALSE. If TRUE the files in pattern will be removed.

verbose

If FALSE, the code is run silently.

Value

  • file_extension(), file_name(), and file_dir() return a character string.

  • manipulate_files() No return value. If verbose == TRUE, a message is printed indicating which operation succeeded (or not) for each of the files attempted.

Examples


library(pliman)
# get file name, directory and extension
file <- "E:/my_folder/my_subfolder/image1.png"
file_dir(file)
file_name(file)
file_extension(file)

# manipulate files
dir <- tempdir()
list.files(dir)
file.create(paste0(dir, "/test.txt"))
list.files(dir)
manipulate_files("test",
                 dir = paste0(dir, "\\"),
                prefix = "chang_",
                save_to = paste0(dir, "\\"),
                overwrite = TRUE)
list.files(dir)


pliman documentation built on Oct. 15, 2023, 1:06 a.m.

Related to utils_file in pliman...