1

I have a folder Tmin which contains 18 folders. Each of the 18 folders contains hundreds of file. I would like to create a program with R that allow to add the name of the folder files for each file. I do not want to rename each of the file with a different name, I only want to add the folder name at the beginning of the file name. I am new in R and in programming. I was not able to have a batch function that can repeat the operation for each folder. You can find attached two pictures, which show what I would like to obtain.

For example, the file called "name_date.tiff" contained in the folder "MACA_Miroc" will become "MACA_Miroc_name_date.tiff". Moreover, I would like to repeat the operation automatically for each folder. Thanks in advance for any help!

Wanted situation and organization of my folders and file

LMontef
  • 41
  • 2
  • 4
  • First, pictures are a bad way to show things. They're not guaranteed to always be available, and some workplaces block file-sharing sites. You can write file trees in text like in [this question](https://stackoverflow.com/questions/1581559/ascii-library-for-creating-pretty-directory-trees). Second, have you tried using `list.files()` and `file.rename()`? – Nathan Werth Feb 02 '18 at 15:52
  • yes, in file .rename, I include list.files. My issue is that I do not know how to ask the program to add the name of my folder to all the files automatically, and that it will repeat the process for all of my folder. I hope my explanations are clear. – LMontef Feb 02 '18 at 16:11
  • The `normalizePath()` function will give you the whole path, but you will have to process that to get the parts you want, remove / or \\ between the directories, and join them using `gsub()` and `paste0()`. You could also use `dir( ., full.names=TRUE, recursive=TRUE)` to get names from your current directory. You still have to process them into valid file names. – dcarlson Feb 02 '18 at 16:33

2 Answers2

1

This ought to work:

mydir <- getwd()  
primary_folder <- "C:/Users/Desktop/Test_Data/"
subfolders <- grep("*MACA*", list.dirs(primary_folder, full.names = T, recursive = F), 
                   value = T)

renameFunc <- function(z){
  setwd(z)
  fnames <- dir(recursive = F, pattern= ".tiff|.csv")
  addname <- substr(z, nchar(primary_folder)+2, nchar(z))

  lapply(fnames, function(current_name){
    #Regex to get extension, may need to addd $ sign to signify end of file name
    ptrn <- ".*\\.([a-zA-Z]{2,4})" 
    extension <- regmatches(current_name, regexec(ptrn, current_name))[[1]][2]
    no_extension <- gsub(paste(".",extension, sep = ""), "", current_name)
    new_name <- paste(gsub("_"," ", no_extension), " ", addname, ".", extension, sep = "")
    file.rename(current_name, new_name)
  })
}

lapply(subfolders, readFunc)

setwd(mydir)

I think if you're not in the directory where you want to change file names, you must specify the full name. Changing the working directory was a quick way but you could use full names (using regular expressions to get the correct from and to values for file.rename()). I got some errors at one poing when I was not in the directory where I wanted to change the name.

I feel this allows more control over which folders you want to change the names in since incorrect operation can be very messy. You may also want to skip some file extensions or subfolders etc.

Gautam
  • 2,597
  • 1
  • 28
  • 51
  • Thanks, I can understand more the reasoning. However, I still do not understand how to add the name of my folder to each of my files such as "name.date". For example, I would like to add MACA_MIROC for all the files contained in the folder MACA_MIROC, MACA_grs for all the files contained in the folder MACA_grs, etc. I can see that to use the function seems to be the best way. – LMontef Feb 02 '18 at 20:44
  • In the code where I define `new_name`, you would need to paste the folder name instead of the string `name`. Folder name could be obtained by subtracting the `primary_folder` from the `subfolder` using `substr`. I'll edit the code above. – Gautam Feb 02 '18 at 22:14
  • I just tried it and it works very well ! Thanks a lot for your help! – LMontef Feb 03 '18 at 02:53
0

Your path folder

folder<-"C:/path/example/"

Extract files list

files<-list.files(folder)

Extract folder name

folder_name<-unlist(strsplit(folder,"/"))[length(unlist(strsplit(folder,"/")))]

Rename all files

file.rename(from = paste0(folder,files),to = paste0(folder,folder_name,"_",files))
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39