Site icon R-bloggers

Renaming all files in a folder in R

[This article was first published on George J. Mount, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I hate the way files are run in a camera. While it was cool to learn for this post that DSCN stands for “Digital Still Capture – Nikon,” it means nothing to me!

For this post, I will be renaming the files that I took from Worden Ledges into a more “human-readable” name.

Ready to “automate the boring stuff with R?” Check out my course, R Explained for Excel Users.

Vectorization for the efficiency

I thought that this would be a loop and even an apply() function, but it turns out all that’s needed is a list of the file names. To rename the files, we will simply list all the current files, list the names of the new files that we want, then switch them around.  

1. List files in the folder

I have saved these photos under C:/Ledgeson my computer. Using the list.files()function, I see them all “listed.”

Actually, this is a vector, not a list, which is its own thing in R. This will make a big difference later on. Too bad that vector.files() doesn’t quite have the same ring!

> old_files <- list.files("C:/Ledges", pattern = "*.JPG", full.names = TRUE)
> old_files
 [1] "C:/Ledges/DSCN7155.JPG" "C:/Ledges/DSCN7156.JPG" "C:/Ledges/DSCN7157.JPG" "C:/Ledges/DSCN7158.JPG"
 [5] "C:/Ledges/DSCN7160.JPG" "C:/Ledges/DSCN7161.JPG" "C:/Ledges/DSCN7162.JPG" "C:/Ledges/DSCN7163.JPG"
 [9] "C:/Ledges/DSCN7164.JPG" "C:/Ledges/DSCN7165.JPG" "C:/Ledges/DSCN7166.JPG" "C:/Ledges/DSCN7167.JPG"
[13] "C:/Ledges/DSCN7168.JPG" "C:/Ledges/DSCN7169.JPG" "C:/Ledges/DSCN7170.JPG" "C:/Ledges/DSCN7171.JPG"
[17] "C:/Ledges/DSCN7172.JPG" "C:/Ledges/DSCN7174.JPG" "C:/Ledges/DSCN7175.JPG" "C:/Ledges/DSCN7176.JPG"
[21] "C:/Ledges/DSCN7177.JPG" "C:/Ledges/DSCN7178.JPG" "C:/Ledges/DSCN7179.JPG" "C:/Ledges/DSCN7180.JPG"
[25] "C:/Ledges/DSCN7181.JPG" "C:/Ledges/DSCN7182.JPG" "C:/Ledges/DSCN7183.JPG" "C:/Ledges/DSCN7184.JPG"
[29] "C:/Ledges/DSCN7185.JPG" "C:/Ledges/DSCN7186.JPG"

2. Create vector of new files

Now we can name all the new files that we want. For example, instead of DSCN7155.JPG, I want a file name like ledges_1.JPG.

Using 1:length(old_files)gives us a vector of the exact same length as old_files.

I have saved these in the folder C:/LedgesR

> new_files <- paste0("C:/LedgesR/ledges_",1:length(old_files),".JPG")
> new_files
 [1] "C:/LedgesR/ledges_1.JPG"  "C:/LedgesR/ledges_2.JPG"  "C:/LedgesR/ledges_3.JPG" 
 [4] "C:/LedgesR/ledges_4.JPG"  "C:/LedgesR/ledges_5.JPG"  "C:/LedgesR/ledges_6.JPG" 
 [7] "C:/LedgesR/ledges_7.JPG"  "C:/LedgesR/ledges_8.JPG"  "C:/LedgesR/ledges_9.JPG" 
[10] "C:/LedgesR/ledges_10.JPG" "C:/LedgesR/ledges_11.JPG" "C:/LedgesR/ledges_12.JPG"
[13] "C:/LedgesR/ledges_13.JPG" "C:/LedgesR/ledges_14.JPG" "C:/LedgesR/ledges_15.JPG"
[16] "C:/LedgesR/ledges_16.JPG" "C:/LedgesR/ledges_17.JPG" "C:/LedgesR/ledges_18.JPG"
[19] "C:/LedgesR/ledges_19.JPG" "C:/LedgesR/ledges_20.JPG" "C:/LedgesR/ledges_21.JPG"
[22] "C:/LedgesR/ledges_22.JPG" "C:/LedgesR/ledges_23.JPG" "C:/LedgesR/ledges_24.JPG"
[25] "C:/LedgesR/ledges_25.JPG" "C:/LedgesR/ledges_26.JPG" "C:/LedgesR/ledges_27.JPG"
[28] "C:/LedgesR/ledges_28.JPG" "C:/LedgesR/ledges_29.JPG" "C:/LedgesR/ledges_30.JPG"

3. Copy from old files to new files

Now all we’ll do is copy the files from the old file locations to the new. A TRUEoutput indicates a successful transfer. 

> file.copy(from = old_files, to = new_files)
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[22] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

Now we can open and see that these have more user-friendly names. 

One nice thing is that because the original files are named sequentially (i.e., DSCN7155 comes before DSCN7156, etc.), so will our new files (i.e., they become ledges_1, ledges_2, etc.).  

4. Clear out the old files

There is no Ctrl + Z on deleting files in R! That’s why I like to copy our files to a new location before deleting the source. We can remove the old files with the file.remove()function.  

> file.remove(old_files)
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[22] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

More than one way to name a file

There are doubtless other (likely even better) ways to do this in R, so how would you do it? One candidate might, for example, be file.path(); however, I found paste0() to work a little more exactly in what I wanted. 

The complete code is below. 

To leave a comment for the author, please follow the link and comment on their blog: George J. Mount.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.