I'm trying to rename the files with .txt extensions in a folder with a corresponding list of names in a column of a table. The table contains two vectors, the first is the heading of the name of the file in the folders, and the second is the actual name that I wish to use retaining the original extension. I can use file rename but how do I replace it with the new name in the corresponding row?
I've tried using a loop with file.rename, except that my code iterates through all the new names in the table with each folder. Not sure if there's an R function that will do this.
library(dplyr) library(stringr) headers <- read.csv(file="/mnt/data/Development/Sequences/SampleSheet.csv", skip = 18, header = F, nrows =1, as.is = T) sample.rows = read.csv(file="/mnt/data/Development/Sequences/SampleSheet.csv", skip = 19, header = F) colnames(sample.rows) = headers old.new.names <- select(sample.rows, Sample_Name, Description) startingDir<-"/mnt/data/Development/Sequences" tcr.sample <- list.files(path=startingDir, pattern="txt", full.names=TRUE ) new.names <- select(old.new.names, Description) file.rename(list.files(tcr.sample, pattern = ".txt" replacement=new.names) Files in the folder have generic names: S01_S1.txt, S02_S2.txt, etc. I also have a file containing a table with 2 columns. The first column identifies each file by the first three characters, such as S05, S06,... S45. The second column has the corresponding new name in for the file in that row, such as RK_ci1151_01, RK_ci1151_02,... RK_ci1151_Baseline. I'm trying to rename the files so that the name is changed to RK_ci1151_01.txt, RK_ci1151_02.. so forth.
I'm also getting a
Error in file.rename(tcr.sample, pattern=".txt", replacement=new.names) : unused arguments (pattern = ".txt, replacement=new.names) message.
pattern=".txt"andreplacement=), so it isn't clear if problems are due to those issues or something else. Can you update based on what you actually are using? (It seems like you're missingsuborgsubcode?)