Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

search - R list.files for multiple specific file names

I have a directory in windows with multiple sub-directories. I also have a list of file names that I would like to search for within the directory structure (to retrieve exact path)

This works fine for a single value as below (current.folder is a variable for the main directory) -

files <- list.files(current.folder, 
                    pattern = test,
                    recursive = TRUE, 
                    full.names = TRUE)

I can then use the returned path to do file.copy.

The problem I am now having is applying that function to multiple file names stored in a dataframe or even a vector.

I've tried referencing them in the pattern argument (which only returns for first value) and used a for loop on a set of just two filenames (returns blank).

Am I using the wrong technique or just not finding the correct setup?

Edit for clarification - test refers to value - "1-5FX3C7P_1-5FX3C8T_JNJLFSPROD-ZZFDA-CDRH-AERS-3500A-01162017131543-1-5FX3C7P.xml"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Does this work:

sapply(test, function(x)list.files(current.folder, 
                                   pattern=x, 
                                   recursive=TRUE,
                                   full.names=TRUE))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...