Skip to content
Related Articles

Related Articles

Improve Article

Rename Files Using R

  • Last Updated : 14 Sep, 2021
Geek Week

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :