To rename a file in R, you can use the “file.rename()” function. It renames a single file name from an old to a new or multiple files simultaneously.
Syntax
file.rename(from, to)
Parameters
- from: It is a character vector that contains all the files to be renamed.
- to: It is a character vector that contains new names for files.
Return value
It returns a logical value. The value is TRUE if the file was successfully renamed and FALSE if the file was not successfully renamed.
Example 1: Renaming a single file
Before renaming a file, we need to check if the file exists or not.
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.
- Pro.R
- old.R
Now, we want to rename the file of old.R to new.R.
The Pro.R is our R program file in which 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.
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, meaning the file is 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 below screenshot to confirm that we successfully renamed a single file.
Example 2: Rename 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 R 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 below code 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 name of three files and now verify by going to the file system.
Example 3: Renaming a file with the full path specified
current_name <- "/Users/krunal/Documents/old_name.csv"
new_name <- "/Users/krunal/Documents/new_name.csv"
file.rename(current_name, new_name)
Example 4: Use regex to rename file names
Syntax
rename.files(dir, pattern, replacement)
Parameters
- dir: It is the path to the directory where you want to change file names.
- pattern: It is a pattern to look for file names in a directory.
- replacement: It is a replacement text when a pattern matches.
Example
rename.files("/your_directory/file", "_", "-")
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.