Rename Files Using R (Example)

 

In this tutorial you’ll learn how to change file names of the files in a working directory on your computer in R.

The content looks as follows:

So now the part you have been waiting for – the tutorial!

Example Files & Working Directory

In the example of this tutorial, we’ll use the following path and working directory as basement:

my_path <- "C:/Users/Joach/Desktop/my directory/"  # Define working directory
my_path                                            # Print path to working directory
# [1] "C:/Users/Joach/Desktop/my directory/"

 

example directory

 

The previous screenshot shows how our example directory looks like. It contains four PNG files with different file names.

 

Example: Rename Files Using file.rename() Function in R

This example illustrates how to modify the files stored in our working directory using the R programming language.

First, we have to get the current names of all files in our directory. For this, we can use the list.files function as shown below:

file_names_old <- list.files(my_path)              # Get current file names
file_names_old                                     # Print current file names
# [1] "another file.png"          "i have many png files.png" "this is a file.png"        "what a nice file.png"

Note that the list.file function provides additional arguments, e.g. you may filter file names depending on a specific file extension. However, in this example we simply rename all files in our folder.

As next step, we have to define the new file names that we want to assign. In this example, we’ll specify a running sequence of file names with the prefix “File No”:

file_names_new <- paste0("File No ",               # Define new file names
                         1:length(file_names_old),
                         ".png")
file_names_new                                     # Print new file names
# [1] "File No 1.png" "File No 2.png" "File No 3.png" "File No 4.png"

The previous output of the RStudio console shows the four new file names that we will use.

As a last step, we can now apply the file.rename function to adjust the names of our files:

file.rename(paste0(my_path, file_names_old),       # Rename files
            paste0(my_path, file_names_new))

After running the previous R syntax, the files in our working directory will look like this:

 

renamed files in directory

 

As you can see, all files have been renamed.

 

Video, Further Resources & Summary

If you need more info on the R syntax of this tutorial, you might want to watch the following video of my YouTube channel. In the video, I illustrate the contents of this article in a live programming session.

 

 

Furthermore, you might want to read the other tutorials on my homepage:

 

Summary: In this R programming tutorial you have learned how to modify PNG file names.

Please note that we could apply the same type of R syntax to rename other file types such as PDF, JPEG, CSV, XLSX, and so on. Furthermore, we could use the R code of this tutorial to rename a large number of files very efficiently.

If you have any additional questions, let me know in the comments section below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


15 Comments. Leave new

  • This didn’t work. is there a difference between your version of R and mine? I am using 4.0.5

    Reply
  • I was missing a “slash” — “/” –at the end of “my_path” parameter. Fixed and works now.

    Reply
  • Hi! I tried using this code, however, it throws me a following error:

    Error in file.rename(paste0(mypath, file_names_old), paste0(mypath, file_names_new)) :
    ‘from’ and ‘to’ are of different lengths

    Reply
    • Hey Shreyas,

      Could you show the content of your data objects mypath, file_names_old, and file_names_new? It seems like something is wrong with them.

      Regards

      Joachim

      Reply
  • Joachim – I am getting the following error message.
    **Note: All my csv files reside on an external hard drive but R has always performed various tasks on my external drive e.g. merging multiple csv files and appending them and many other tasks.
    As soon as I run this line of code I get the warning message:
    > file.rename(paste0(my_path, file_names_old), # Rename files
    + paste0(my_path, file_names_new))
    [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
    [17] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
    Warning messages:
    1: In file.rename(paste0(my_path, file_names_old), paste0(my_path, … :
    cannot rename file ‘E:/01-Password Protected Docs/R Studio Projects/CSV_EXCEL_Datasets/csv filesAutoCheckUsage.csv’ to ‘E:/01-Password Protected Docs/R Studio Projects/CSV_EXCEL_Datasets/csv filesCSV_File_No 1.csv’, reason ‘The system cannot find the file specified’

    please help !!

    Reply
  • No errors, but here is a warning–the R coding here uses a different method to sort the directory with the files you are renaming than Microsoft Explorer uses. For instance, if I sort on Name in Explorer, my first few files are named “3K1-X, etc.” But, when I use this R coding, the first files listed are “12D-XX..etc.” Looks like Explorer lists files starting with “3” before files starting with “12”. I had a similar problem with files “Filename14” being listed AFTER “FileName100” in the R coding (file_names_old lists the files renamed in the order they will be renamed when file_rename is executed).

    Reply
  • Hi Joachim,

    Thank you for this tutorial. However, upon running the first line of this code I get:
    > my_path <- C:/Users/Eduar/Desktop/rename_pq/
    Error: unexpected input in “my_path <”

    Any idea what the issue is? Must I install/load a specific library for this?

    Thanks and cheers,

    Reply
  • Thanks Cansu,
    Well spotted. But I actually did run it with quite marks. Since it was not working i did try again without.

    So im afraid the issue lies elsewhere.

    Im quite an R illiterate, so its quite possible the right package isnt installed or loaded, but im not sure how to verify this. Suggestions?

    Thank you

    Reply
  • Hi Cansu,

    Thank you. The “\” wasnt an issue after all. I just found that there was a space between “<" and "-".

    By writing <- instead of < -, things are now working.

    cheers,

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top