Move Files Between Folders in R (2 Examples)
In this article, you’ll learn how to move files between two directories in R programming.
Table of contents:
Let’s get started!
Creating Example Data
Suppose that we have stored two folders on our computer which look like follows:
As you can see, the folder on the left side called “my directory a” contains three files: A TXT file, an XLSX file, and a Microsoft Word document. The folder called “my directory b” that is shown on the right side is empty.
Now, let’s assume that we want to move all files of the first folder to the second folder.
Then, we first have to create a data object containing the names of all files in our first working directory.
We can do this by applying the list.files() function as shown below:
# List of all file names my_files <- list.files("C:/Users/Joach/Desktop/my directory a") my_files #  "my file 1.txt" "my file 2.xlsx" "my file 3.docx"
As you can see based on the previous output of the RStudio console, the previous R code has created a vector object containing the file names in our first example folder.
Let’s move those files!
Example 1: Copy Files to New Folder Using file.copy() Function
In Example 1, I’ll explain how to create a duplicate of data files stored in a directory in another directory.
To achieve this, we can use the file.copy() function as shown in the following R syntax.
Within the file.copy function, we have to specify the directory path and file names of the first folder from which we want to copy the files, as well as the directory path and file names of the second folder to which we want to copy the files.
Have a look at the following R code:
# Create copy of files file.copy(from = paste0("C:/Users/Joach/Desktop/my directory a/", my_files), to = paste0("C:/Users/Joach/Desktop/my directory b/", my_files))
After executing the previous syntax, our two folder look as shown below:
As you can see, directories contain exactly the same data files with the same names.
We could stop at this point both, if we want to keep a copy of our files in both folders. However, in the next example, I’ll explain how to remove the files from the original folder and keep them only in the new folder.
Example 2: Remove Original Files from Old Folder Using file.remove() Function
In this section, I’ll illustrate how to delete all files that have been moved from one folder to another.
For this task, we can apply the file.remove function as shown in the following R syntax:
# Remove original files file.remove(from = paste0("C:/Users/Joach/Desktop/my directory a/", my_files))
Let’s have a look at our directories once again:
The files in the original folder have been deleted and are now only stored in the new folder.
Video & Further Resources
In case you need further explanations on the topics of this article, I recommend having a look at the following video on my YouTube channel. In the video, I show the R codes of this tutorial.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to have a look at the related articles on this website. Please find a selection of articles on how to switch file names, handling directories and paths, and dealing with different file extensions below.
- How to Delete a File Using R
- Rename Files Using R
- Remove Extension from File Name
- Extract File & Directory Name from Path
- R Programming Language
In summary: You have learned in this article how to change the file location from one folder to another in the R programming language. In case you have additional questions and/or comments, let me know in the comments below.
Statistics Globe Newsletter