2

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.

4
  • Technically that's an error, not a message. There are some syntax errors in your code here (e.g., no comma between pattern=".txt" and replacement=), 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 missing sub or gsub code?) Commented Jul 22, 2019 at 17:51
  • need comma between pattern= and replacement= ? Commented Jul 22, 2019 at 17:51
  • Sorry, there is a comma in the original code. I forgot to include it during the message transcription. Here's the actual line: file.rename(tcr.sample, pattern = ".txt", replacement= new.names). The "new.names" is actually a variable that contains a vector with all the new names by row. Commented Jul 22, 2019 at 17:54
  • I suspect I'm not using the correct pattern, as .txt is actually the only part of the file that I want to keep. I apologize. I'm a newbie to R and I'm not sure how the file.rename actually works. Commented Jul 22, 2019 at 17:57

2 Answers 2

4
# Script to replace the standard Iseq100 output sample names with name # in the "Description" column in the SampleSheet.csv file. library(dplyr) library(stringr) # Set the working directory to folder where sample files (.txt) are located. setwd("/mnt/data/Development/Sequences") # Extract the headers of the sample table in the file. headers <- read.csv(file="SampleSheet.csv", skip = 18, header = F, nrows =1, as.is = T) # Extract the sample rows. sample.rows = read.csv(file="SampleSheet.csv", skip = 19, header = F) # Add the headers to the sample rows colnames(sample.rows) = headers # Extract the "Descrription" column which contains the actual names of the sample new.names <- select(sample.rows, Description) new.names <- paste0(new.names$Description, '.txt') # Extract target .txt files and rename to Description name. old.names <- list.files(path=".", pattern=".txt") file.rename(from=old.names, to=new.names) 
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can achieve the file renaming with a different approach. If your CSV file lists the unique file names that you want and they are associated with a unique 'grouping' variable (in your case, 'S01' is associated with files RK_ci1151_01, RK_ci1151_02, RK_ci1151_Baseline), then you can use the new names to recreate the old names. In other words, you can substitute the pattern before '_01.txt', '_02.txt', etc. in the new file names with the pattern of the old file names. Then use the columns of the dataframe as the from= and to= arguments in the file.rename call.

### prep toy data # create df with old and new names df <- data.frame(old=paste0(rep(letters[1:3],each=3), '_', rep(c(0:2),3), '.txt'), new=paste0(rep(c('foo','bar','hello'),each=3), '_', rep(c(0:2),3), '.txt'), stringsAsFactors = F) # write files with old names for (i in 1:length(df$old)) { write.table(NULL,df$old[i]) } list.files(pattern='\\.txt') [1] "a_0.txt" "a_1.txt" "a_2.txt" "b_0.txt" "b_1.txt" "b_2.txt" "c_0.txt" "c_1.txt" "c_2.txt" # edit old names to match user code df$old <- sub('_[0-9]\\.txt','',df$old) > df old new 1 a foo_0.txt 2 a foo_1.txt 3 a foo_2.txt 4 b bar_0.txt 5 b bar_1.txt 6 b bar_2.txt 7 c hello_0.txt 8 c hello_1.txt 9 c hello_2.txt # separate new file names to join with old df$join <- sub('.*(_[0-9]\\.txt)','\\1',df$new) df$old1 <- paste0(df$old,df$join) # rename file.rename(df$old1, df$new) list.files(pattern='\\.txt') [1] "bar_0.txt" "bar_1.txt" "bar_2.txt" "foo_0.txt" "foo_1.txt" "foo_2.txt" [7] "hello_0.txt" "hello_1.txt" "hello_2.txt" 

2 Comments

kstew, your suggestion didn't quite work as I had hoped, but it pointed me in the right direction. I'm posting my solution below. Thank you.
Next time, it will be helpful to include a min reprex in your question, reprex.tidyverse.org/articles/reprex-dos-and-donts.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.