How to rename a file name in R Programming? To rename a file name use file.rename()
function, by using this you can rename a single file name from an old to a new or multiple files at a time. When renaming multiple files at a time, use Vector to specify the old and new names.
To replace all files in a directory by matching regex expression use rename.files()
function. In this R Programming article, I will explain how to change the file name, change multiple file names and finally replace a text in a file name with another text.
Related: How to delete a file or directory in R?
1. file.rename() – Rename File Name
Use file.rename()
function to rename a file name in R programmatically, by using this you can either change a single file name or change multiple file names at a time by specifying from and to params as vectors.
1.1 Syntax of file.rename()
The following is the syntax of the file.rename()
function.
# Syntax of file.rename()
file.rename(from, to)
Parameters:
from
: From file name. Also takes Vectorto
: To file name. Also takes Vector.
1.2 Rename Single File Name
By specifying the text value to from and to params you can rename a single file name. The following example first checks if the CSV file exists using file.exists()
, when the file exists it renames from file_name.csv
to file-name.csv
.
Note that if you try to rename a file that doesn’t exist, it returns a warning message “cannot rename file ‘file_name.csv’ to ‘file-name.csv’, reason ‘No such file or directory'” hence, you need to check if file exists using a file.exists()
function.
# File name to rename
filename <- 'file_name.csv'
# Check if file exists.
if(file.exists(filename)){
# Rename file name
file.rename(filename,'file-name.csv')
}else{
print('File Not found :')
}
1.3 Rename Multiple File Names
By using the same function you can change multiple file names, To do so you need to specify the vectors for from
and to
params. It changes the file name element-wise, the first element from
vector to the first element on to
vector.
# Replace Multiple Files Names
from_files <- c('file_name1.txt','file_name2.txt','file_name3.txt')
to_files <- c('file-name1.txt','file-name2.txt','file-name3.txt')
file.rename(from_files,to_files)
Here, the c() function is used to create a Vector.
2. rename.files() – Uses Regex to Rename File Names
The rename.files()
function in R is used to rename all file names recursively from a directory by using a regex expression. This function prints Done! to the console after renaming all files. You need to check the directory if the changes to the names have applied as expected.
2.1 Syntax of rename.files()
The following is the syntax of the rename.files()
function.
# Syntax of rename.files()
rename.files(dir, pattern, replacement)
Parameters:
dir
: Path to the directory where you wanted to change file names.pattern
: Pattern to look for file names in a directory.replacement
: Replacement text when a pattern match.
2.2 Rename File Names in a Directory
By using the rename.files()
function let’s see how to rename all files that match the pattern in a directory. The below examples change file names that have a text _ (underscore) to – (hyphen). For example, changing the file name from file_name.txt
to file-name.txt
.
# Replace text in file names
rename.files('/apps/directory','_','-')
3. Complete Example of Rename File in R
The following is a complete example of how to change file names in R.
# Example 1
# File name to rename
filename <- 'file_name.csv'
# Check if file exists.
if(file.exists(filename)){
# Rename file name
file.rename(filename,'file-name.csv')
}else{
print('File Not found :')
}
# Example 2
# Replace Multiple Files Names
from_files <- c('file_name1.txt','file_name2.txt','file_name3.txt')
to_files <- c('file-name1.txt','file-name2.txt','file-name3.txt')
file.rename(from_files,to_files)
# Example 3
# Using rename.files()
# Replace text in file names
rename.files('/apps/directory','_','-')
4. Conclusion
In this article, you have learned how to rename file names in R by using file.rename() and rename.files() functions. file.rename() is used to rename single and multiple file names and rename.files() is used to rename all files in a directory by pattern matching.
Related Articles
- R – Replace NA values with Zero
- R – Replace NA with Empty String
- R – Replace Character in a String
- Different ways to Rename Column Names.
- Use unlink() to delete a file in R
- R solve() Equation with Examples
- Pipe in R with Examples
- R lm() Function – Fitting Linear Models
- outer() Function in R with Examples
- Running Time of R Code & Function