Batch-rename files
batch-rename.RmdThis vignette covers grp_batch_rename(), a function that
gives you the ability to rename a folder of files in a standardized
format.
Test files
Below we’ll create a folder of messy file names in a temporary folder.
tmp_files <- file.path(tempdir(), "filenames")
dir.create(tmp_files)
messy <- c(
"JW7d^(2sl@deletethisandyourcareerisoverWx2.txt",
"Joe's Filenames Use Space and Punctuation.xlsx",
"fig 2.png",
"figure 1.png",
"myabstract.docx"
)
file.create(file.path(tmp_files, messy))
#> [1] TRUE TRUE TRUE TRUE TRUEConfirm this with gerp::grp_path()
gerp::grp_path(tmp_files, tree = TRUE)
#> /tmp/Rtmp85NEFu/filenames
#> ├── JW7d^(2sl@deletethisandyourcareerisoverWx2.txt
#> ├── Joe's Filenames Use Space and Punctuation.xlsx
#> ├── fig 2.png
#> ├── figure 1.png
#> └── myabstract.docx
grp_batch_rename()
The files in the temporary filenames/ folder are a mess,
and I want to rename them using a standardized
format. Renaming files manually can be time-consuming, so
grp_batch_rename() will get you 90% there:
all file names are converted to lowercase words separated by hyphens (the same rules as
grp_fname())all punctuation and special characters are removed from file names
file names have a date prefix (the
"modification"date, but other options include"birth","access", or"change")files that already have a date prefix are skipped
Preview the new names with dry_run = TRUE:
preview <- gerp::grp_batch_rename(tmp_files, dry_run = TRUE)
basename(preview$new)
#> [1] "2026-09-25_jw7d-2sl-deletethisandyourcareerisoverwx2.txt"
#> [2] "2026-09-25_joes-filenames-use-space-and-punctuation.xlsx"
#> [3] "2026-09-25_fig-2.png"
#> [4] "2026-09-25_figure-1.png"
#> [5] "2026-09-25_myabstract.docx"Then rename the files:
gerp::grp_batch_rename(tmp_files)
#> ✔ Renamed 5 files in /tmp/Rtmp85NEFu/filenamesAnd confirm with gerp::grp_path()
gerp::grp_path(tmp_files, tree = TRUE)
#> /tmp/Rtmp85NEFu/filenames
#> ├── 2026-09-25_fig-2.png
#> ├── 2026-09-25_figure-1.png
#> ├── 2026-09-25_joes-filenames-use-space-and-punctuation.xlsx
#> ├── 2026-09-25_jw7d-2sl-deletethisandyourcareerisoverwx2.txt
#> └── 2026-09-25_myabstract.docxNOTE: grp_batch_rename() can’t change
the content of the files (and you wouldn’t
want it to).