Open In App

Rename Files Using R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to rename files using R Programming Language.

To rename a file in R we use file.rename(). This function renames all the files mentioned as parameters. It returns normally TRUE for success, FALSE for failure. 

Note: File naming conventions depend on the platform.

Syntax: file.rename( to, from)

Parameters:

  • to: character vector that contains all the files to be renamed
  • from: character vector that contains new names for file

File for demonstration:

Directory

Below is the implementation:

Here we first list out the working directory and then use rename() function to rename the file name.

R




# Define working directory
working_directory <- "working/dic/"
  
# get list of current file
# names as character vector current_files
current_files <- list.files(working_directory)
  
# get file names after renaming
# as character vector new_files
new_files <- c("geeks1.txt","geeks2.txt")
  
# Use file.rename() function to rename files
file.rename(paste0(working_directory, current_files),
            paste0(working_directory, new_files))


Output:

TRUE TRUE

Final Directory


Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads