Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

rename_seq

Rename files with a sequential numeric prefix


Description

Rename a series of files and add an incremental numeric prefix to the filenames. For example, files ‘a.txt’, ‘b.txt’, and ‘c.txt’ can be renamed to ‘1-a.txt’, ‘2-b.txt’, and ‘3-c.txt’.

Usage

rename_seq(
  pattern = "^[0-9]+-.+[.]Rmd$",
  format = "auto",
  replace = TRUE,
  start = 1,
  dry_run = TRUE
)

Arguments

pattern

A regular expression for list.files() to obtain the files to be renamed. For example, to rename .jpeg files, use pattern = "[.]jpeg$".

format

The format for the numeric prefix. This is passed to sprintf(). The default format is "%0Nd" where N = floor(log10(n)) + 1 and n is the number of files, which means the prefix may be padded with zeros. For example, if there are 150 files to be renamed, the format will be "%03d" and the prefixes will be 001, 002, ..., 150.

replace

Whether to remove existing numeric prefixes in filenames.

start

The starting number for the prefix (it can start from 0).

dry_run

Whether to not really rename files. To be safe, the default is TRUE. If you have looked at the new filenames and are sure the new names are what you want, you may rerun rename_seq() with dry_run = FALSE) to actually rename files.

Value

A named character vector. The names are original filenames, and the vector itself is the new filenames.

Examples

xfun::rename_seq()
xfun::rename_seq("[.](jpeg|png)$", format = "%04d")

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.