Search code examples
rdirectoryrenamefile-rename

Renaming files and directories with the same pattern in R


I am trying to rename a number of files and folders with a new name.

Example old name: corrected_original_wh_ah108090.pdf

Example new name: corrected_original_gsmp01358_108090.pdf

Example old path: Data/Test2/ARGOS/wh_ah108090/crawl/corrected_original_wh_ah108090.pdf

Example new path:Data/Test2/ARGOS/gsmp01358_108090/crawl/corrected_original_gsmp01358_108090.pdf

Example metadata:

old         new
wh_ah108090 gsmp01358_108090
wh_ah108091 gsmp01359_108091
wh_ah108092 gsmp01360_108092
wh_ah108093 gsmp01361_108093
wh_ah108096 gsmp01362_108096
wh_ah108102 gsmp01363_108102
wh_ah108106 gsmp01364_108106

Code:

# Read metadata for ID's #
meta <- read.csv('Metadata.csv')


# list all file paths
pathLs <- list.files('Data/Test2/', recursive = TRUE, full.names = TRUE)


# select only files with old format on the list (for full dataset where some files already have new name)
tbl<- pathLs [!grepl("gsmp", pathLs )]

# select only files with old format on metadata
metadata<- meta[!meta$old =="",]

# function to change old names for new
fileList <- apply(metadata,1, 
                 function(x) {
                 fnam <- x['old']
                 fnam <- as.character(unlist(fnam))


                 newnam <- gsub(fnam, as.character(unlist(x['new'])), tbl[grepl(fnam, tbl)])
                 return(newnam)})

# Create dataframe with old and new names

to <- as.character(unlist(fileList))
from <- tbl 

# Use rename 
file.rename(from, to)

For some reason this file rename doesn't work. Is this because I cannot rename files and directories in a path at the same time?


Solution

  • No loops required.

    metadata <- read.table(header=T, stringsAsFactors=F, text="
    old         new
    wh_ah108090 gsmp01358_108090
    wh_ah108091 gsmp01359_108091
    wh_ah108092 gsmp01360_108092
    wh_ah108093 gsmp01361_108093
    wh_ah108096 gsmp01362_108096
    wh_ah108102 gsmp01363_108102
    wh_ah108106 gsmp01364_108106")
    metadata$new2 <- sprintf("gsmp%05d_%s",
                             1357L + seq_len(nrow(metadata)), # 1357 can be anything?
                             gsub("\\D", "", metadata$old))
    metadata
    #           old              new             new2
    # 1 wh_ah108090 gsmp01358_108090 gsmp01358_108090
    # 2 wh_ah108091 gsmp01359_108091 gsmp01359_108091
    # 3 wh_ah108092 gsmp01360_108092 gsmp01360_108092
    # 4 wh_ah108093 gsmp01361_108093 gsmp01361_108093
    # 5 wh_ah108096 gsmp01362_108096 gsmp01362_108096
    # 6 wh_ah108102 gsmp01363_108102 gsmp01363_108102
    # 7 wh_ah108106 gsmp01364_108106 gsmp01364_108106
    
    file.rename(metadata$old, metadata$new2) # should do it