This tutorial shows you how you can rename files using R. This tutorial will show you how you can use R to rename many files in an easy way.

How do I rename a batch of files (many files) in one go?

This tutorial is aimed at beginners and intermediate users of R with the aim of showcasing how to rename many files in a folder in one go using R. The aim is not to provide a fully-fledged analysis but rather to show a useful method for getting this done.

The entire R Notebook for the tutorial can be downloaded here. If you want to render the R Notebook on your machine, i.e. knitting the document to html or a pdf, you need to make sure that you have R and RStudio installed and you also need to download the bibliography file and store it in the same folder where you store the Rmd or the Rproj file.

The aim of the task here is to take all files ending in .Rmd, then adding *_cb* to their name, and then saving them in a folder called cbs which is a subfolder of the current working directory. So, if there is a file called test.Rmd, we want to change its name to test_cb.Rmd and then save that renamed file in the cbs folder.

Preparation and session set up

This tutorial is based on R. If you have not installed R or are new to it, you will find an introduction to and more information how to use R here. For this tutorials, we need to install certain packages from an R library so that the scripts shown below are executed without errors. Before turning to the code below, please install the packages by running the code below this paragraph. If you have already installed the packages mentioned below, then you can skip ahead and ignore this section. To install the necessary packages, simply run the following code - it may take some time (between 1 and 5 minutes to install all of the libraries so you do not need to worry if it takes some time).

# install packages
# install klippy for copy-to-clipboard button in code chunks

Now that we have installed the packages, we activate them as shown below.

# set options
options(stringsAsFactors = F)          # no automatic data transformation
options("scipen" = 100, "digits" = 12) # suppress math annotation
# activate packages
# activate klippy for copy-to-clipboard button

Once you have installed R and RStudio and also initiated the session by executing the code shown above, you are good to go.

Getting started

We begin by creating a list of the files you want to rename.

You need to change the path that is used in the code below and include the path to the files on your computer! The code below will create a list of the files in the current working directory that end in “.Rmd”.

# list files
fnames  <- list.files(here::here(""), pattern = ".Rmd", full.names = T)

Let’s briefly inspect file names.

The inspection shows that the file names represent the location of the files on my computer. In a next step, we load the file content in a vector called fcontent.

fcontent <- sapply(fnames, function(x){
  x <- readLines(x)
  x <- paste0(x, sep = "\n", collapse = "")

Next, we edit the file names by only retaining the file name itself, removing the .Rmd- ending and then adding the desired *_cb.Rmd* ending.

fnames <- fnames %>%
  stringr::str_remove_all(".Rmd") %>%
  stringr::str_remove_all(".*/") %>%
  paste0(., "_cb.Rmd")

Let’s inspect file names again to see if changing the file names was successful.

We now save the files with a new name into the folder where we want them to be. To this end, we need to specify the con (the connection, i.e., the place where we want to store the file). In the present case, we extract the path to the working directory, then add cbs/ and then add the edited file name.

for (i in 1:length(fcontent)) {
  writeLines(text = fcontent[i],
    con = paste0(here::here("cbs"), "/", fnames[i], sep = ""))

One the code above is executed, the files were all in the cbs subfolder as desired.

Citation & Session Info

Schweinberger, Martin. 2022. Renaming files with R. Brisbane: The University of Queensland. url: (Version 2022.05.21).

  author = {Schweinberger, Martin},
  title = {Renaming files with R},
  note = {},
  year = {2022},
  organization = "The University of Queensland, School of Languages and Cultures},
  address = {Brisbane},
  edition = {2022.05.21}
## R version 4.2.0 (2022-04-22 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19043)
## Matrix products: default
## locale:
## [1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
## [3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
## [5] LC_TIME=German_Germany.utf8    
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## other attached packages:
##  [1] flextable_0.7.0 here_1.0.1      forcats_0.5.1   stringr_1.4.0  
##  [5] dplyr_1.0.9     purrr_0.3.4     readr_2.1.2     tidyr_1.2.0    
##  [9] tibble_3.1.7    ggplot2_3.3.6   tidyverse_1.3.1 knitr_1.39     
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.8.3      lubridate_1.8.0   assertthat_0.2.1  rprojroot_2.0.3  
##  [5] digest_0.6.29     utf8_1.2.2        R6_2.5.1          cellranger_1.1.0 
##  [9] backports_1.4.1   reprex_2.0.1      evaluate_0.15     httr_1.4.3       
## [13] highr_0.9         pillar_1.7.0      gdtools_0.2.4     rlang_1.0.2      
## [17] uuid_1.1-0        readxl_1.4.0      data.table_1.14.2 rstudioapi_0.13  
## [21] jquerylib_0.1.4   klippy_0.0.0.9500 rmarkdown_2.14    munsell_0.5.0    
## [25] broom_0.8.0       compiler_4.2.0    modelr_0.1.8      xfun_0.30        
## [29] systemfonts_1.0.4 base64enc_0.1-3   pkgconfig_2.0.3   htmltools_0.5.2  
## [33] tidyselect_1.1.2  fansi_1.0.3       crayon_1.5.1      tzdb_0.3.0       
## [37] dbplyr_2.1.1      withr_2.5.0       grid_4.2.0        jsonlite_1.8.0   
## [41] gtable_0.3.0      lifecycle_1.0.1   DBI_1.1.2         magrittr_2.0.3   
## [45] scales_1.2.0      zip_2.2.0         cli_3.3.0         stringi_1.7.6    
## [49] renv_0.15.4       fs_1.5.2          xml2_1.3.3        bslib_0.3.1      
## [53] ellipsis_0.3.2    generics_0.1.2    vctrs_0.4.1       tools_4.2.0      
## [57] glue_1.6.2        officer_0.4.2     hms_1.1.1         fastmap_1.1.0    
## [61] yaml_2.3.5        colorspace_2.0-3  rvest_1.0.2       haven_2.5.0      
## [65] sass_0.4.1

Back to top

Back to HOME