file.rename(): Renaming Single and Multiple Files in R

To rename a file in R, you can use the file.rename() function. It renames a single file or multiple files simultaneously from an old name to a new one.

It returns a logical value. The value is TRUE if the file was successfully renamed, and FALSE if it was not.

Syntax

file.rename(from, to)

Parameters

Argument Description
from It represents a character vector that contains all the files to be renamed.
to It represents a character vector that contains new names for files.

 Renaming a single file

Before renaming a file, we need to check if the file exists.

To check if the file exists, use the file.exists() method.

Let’s say we have a directory named “data” and in that directory, we have two files.

  1. Pro.R
  2. old.R

Now, we want to rename the file of old.R to new.R. 

The Pro.R is our R program file, where we write code.

To list files in the current working directory, use the list.files() method.

Write this method inside the Pro.R file.

list.files()

# Output: Pro.R old.R

Check out this screenshot to verify that we have only two files.

Screenshot of single file

Write a code inside Pro.R file that renames the file from the old.R to new.R.

old_file <- "old.R"
new_file <- "new.R"

if (file.exists(old_file)) {
  file.rename(old_file, new_file)
} else {
  cat("The file does not exist")
}

list.files()

# Output:

# [1] TRUE
# [1] "Pro.R" "new.R"

It returns TRUE, indicating that the file has been successfully renamed to data.R.

The file.rename() function will only work if the current file exists and is not currently open. If the file is open, you must close it before renaming it.

Let’s check out the screenshot below to confirm that we successfully renamed a single file.

Screenshot of renaming a single file in R

Renaming multiple files

To rename multiple files, use the file.rename() with a vector of old names and a vector of new names.

You can also use the list.files() with a pattern argument to select files that match a certain pattern.

Let’s say we have a newDir directory with four files.

Screenshot of list.files() function to get list of all files

To list all the files in the current directory, use the list.files() function.

Write this function inside the fourth Pro.R file, as seen in the above screenshot.

list.files()

# Output: [1] "Pro.R" "main_app.R" "main_data.R" "main_index.R"

Now, we will rename three files (“main_app.R”, “main_data.R”, and “main_index.R”) using the file.rename() method.

Write the code below inside the Pro.R file.

library(stringr)

file.rename(list.files(pattern = "main"), 
str_replace(list.files(pattern = "main"), pattern = "main", "my"))

list.files()

# Output:
# [1] TRUE TRUE TRUE
# [1] "Pro.R" "my_app.R" "my_data.R" "my_index.R"

We successfully changed the names of three files, and now verify by going to the file system.

Screenshot of renaming multiple files in R

Renaming a file with the full path specified

In this case, you need to pass the full path of the source and destination.

current_name <- "/Users/krunal/Documents/old_name.csv"

new_name <- "/Users/krunal/Documents/new_name.csv"

file.rename(current_name, new_name)

That’s it.

Leave a Comment