How to Move File from One Folder to Another in R

To move a file from one folder to another in R, you can use the file.rename() function. This function takes two arguments – the source file and the destination folder – and moves the file from the source folder to the destination folder. The source file can be specified using a file path, while the destination folder can be specified using either a local directory path or a URL. The file.rename() function returns a Boolean value indicating whether the operation was successful or not.


You can use the following methods to move a file from one folder to another in R:

Method 1: Move File from One Folder to Another (And Keep Original File)

file.copy(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
          to="C:/Users/bob/Documents/new_data/soccer_data.csv")

This particular syntax copies a file called soccer_data.csv located in the following folder:

C:/Users/bob/Documents/current_data

Then this file is copied into the following folder:

C:/Users/bob/Documents/new_data

Since we used the file.copy() function, the original file is still kept in the folder it came from.

Method 2: Move File from One Folder to Another (And Delete Original File)

file.rename(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
            to="C:/Users/bob/Documents/new_data/soccer_data.csv")

This particular syntax copies a file called soccer_data.csv located in the following folder:

C:/Users/bob/Documents/current_data

Then this file is copied into the following folder:

C:/Users/bob/Documents/new_data

Since we used the file.rename() function, the original file is deleted from the folder it came from.

The following examples show how to use each method in practice.

Related:

Example 1: Move File in R (And Keep Original File)

Suppose we want to move a file called soccer_data.csv located in the following folder:

C:/Users/bob/Documents/current_data

The folder currently has three files in it:

We can use the following syntax in R to move soccer_data.csv into a new folder called new_data:

file.copy(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
          to="C:/Users/bob/Documents/new_data/soccer_data.csv")

[1] TRUE

The file.copy() function returns a value of TRUE, which tells us it worked.

If we open the new_data folder, we can see that the soccer_data.csv file has indeed been copied into it:

And if we return to the folder where the file came from, we will see that it still exists there.

Note: If either of the file paths is incorrect, the file.copy() function will instead return a value of FALSE.

Example 2: Move File in R (And Delete Original File)

We can use the following syntax in R to move soccer_data.csv into a new folder called new_data and delete the original file:

file.rename(from="C:/Users/bob/Documents/current_data/soccer_data.csv",
            to="C:/Users/bob/Documents/new_data/soccer_data.csv")

[1] TRUE

The file.rename() function returns a value of TRUE, which tells us it worked.

If we open the new_data folder, we can see that the soccer_data.csv file has indeed been copied into it:

And if we return to the folder where the file came from, we will see that it has been deleted from that folder:

The following tutorials explain how to perform other common tasks in R:

x