Skip to contents

Rename files and/or directories using a syntactic naming function

Usage

syntacticRename(
  path,
  recursive = FALSE,
  fun = c("kebabCase", "snakeCase", "camelCase", "upperCamelCase"),
  quiet = FALSE,
  dryRun = FALSE
)

Arguments

path

character. File and/or directory paths.

recursive

logical(1). Should the function recurse into directories?

fun

character(1). Function name.

quiet

logical(1). Perform command quietly, suppressing messages.

dryRun

logical(1). Return the proposed file path modifications without modification.

Value

list. Named list containining from and to rename operations.

Details

Intelligently deals with a case-insensitive file system, if necessary. This is very useful for macOS and Windows.

Our syntactic naming functions can result in changes that only differ in case, which are problematic on case-insensitive mounts, and require movement of the files into a temporary file name before the final rename.

Note

Updated 2023-02-06.

Examples

testdir <- AcidBase::tempdir2()
from <- file.path(testdir, c("helloWorld.txt", "fooBar.R"))
file.create(from)
#> [1] TRUE TRUE
print(basename(from))
#> [1] "helloWorld.txt" "fooBar.R"      
output <- syntacticRename(from)
#> → Renaming /private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/helloWorld.txt to /private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/helloworld.txt.
#> → Renaming /private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/fooBar.R to /private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/foobar.R.
print(output)
#> $from
#> [1] "/private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/helloWorld.txt"
#> [2] "/private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/fooBar.R"      
#> 
#> $to
#> [1] "/private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/helloworld.txt"
#> [2] "/private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpZXXylt/L2ivMC4oyX-169636177907393/foobar.R"      
#> 
AcidBase::unlink2(testdir)