#I would wish that my snippet is corrected. many thanks
#Example of filename in a directory:: 02_ZR66_042_FRT1008.fcs
# Function to rename files
rename_files <- function(directory) {
# Retrieve the basenames of the files in the directory
files <- list.files(directory, full.names = TRUE)
filenames <- basename(files)
## Regular expression pattern to remove the initial part
pattern <- "\\d{2}_ZR\\d{2}_\\d{3}_"
## Regular expression pattern to extract the desired substring
pattern2 <- "[A-Za-z0-9]{8}"
## Apply renaming to each file
for (file in filenames) {
# Extracting the desired substring from the file name
result <- str_remove(file, pattern)
result2 <- str_extract(result, pattern2)
# Ensure the extension is maintained
new_file_name <- paste0(directory, result2, ".fcs")
# Renaming the file
file.rename(file.path(directory, file), file.path(directory, new_file_name))
}
}
## Apply the renaming function to the specified directory
rename_files(“directory”)
#output
#Warning messages:
#1: In file.rename(file.path(directory, file), file.path(directory, :
# cannot rename '..../.fcs' to '..../NA.fcs’ , reason 'No such file or directory'
Your example has only seven characters before the .fcs
file extension — could that be the problem?
Changing it to 7 didn’t solve the problem
Hi @Farm , maybe is better is you provide a better example of the file names.
Try to paste the a df
when you get list.files()
.
files_list <- data.frame(folder_path=list.files(path = 'D:\\Put_you_path\\Desktop\\folder_name',
recursive = TRUE,
full.names = TRUE,
pattern = '\\.fcs))
Not sure a data frame is necessary, but a simple copy-paste of the directory contents — or at least enough so it includes file names that trigger an error — would be helpful.
I made the following changes and it all works now.
library(stringr)
#Function to rename files
rename_files <- function() {
files_list <- list.files(path = "directory",
recursive = TRUE,
full.names = TRUE,
pattern = '\\.fcs')
# Convert list of files into a one-column data frame
files_df <- data.frame(folder_path = files_list)
#Regular expression pattern to remove the initial part
pattern <- "\\d{2}_TA\\d{2}_\\d{3}_"
#Regular expression pattern to extract the desired substring
pattern2 <- "[A-Za-z0-9]{7}"
#Apply renaming to each file
for (file in files_df$folder_path) {
#Extracting the desired substring from the file name
result <- str_remove(basename(file), pattern)
result2 <- str_extract(result, pattern2)
#Ensure the extension is maintained
new_file_name <- paste0(result2, ".fcs")
# Renaming the file, in the proper directory
file.rename(file, new_file_name)
}
}
#Apply the renaming function to the specified directory
rename_files()
}
}
directory= “actual_directory"
rename_files("directory")
You seem to have an issue with unusual quotation marks
your first use of them ; consider the first quote mark symbol
It should be :
path = "directory"
what you have :
path = “directory"
which I wouldnt expect to run
In my view of the code formatting on this forum (I use darkmode) I can see the code is white text, and where there is text within quotations its highlighted in pink, and after the section I highlighted to you everything goes pink...
Thanks for pointing that out
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.