List.files - exclude folder

I want to use the R function list.files to search for all text files in a folder and in its subfolders. However, I would like to exclude all files that are in the same subfolder, say, contains unfinished work, which is not ready for what I am using other files for. The structure looks like this:

- folder |- subfolder_1_good_stuff |- subfolder_2_good_stuff |- subfolder_3_good_stuff |- subfolder_4_unfinished_stuff 

So, the β€œfolder” will be my working directory.

Now I would use:

 list.files(path=".", pattern=".txt", recursive=TRUE) 

But what should I add to the expression "path" to exclude the folder with the unfinished material. This folder name will not be present in any file names, if that matters.

+5
source share
1 answer

Use regex - grepl to exclude:

 myfiles <- list.files(path = ".", pattern = ".txt", recursive = TRUE) myfilesfinished <- myfiles[ !grepl("unfinished_stuff", myfiles) ] 
+6
source

Source: https://habr.com/ru/post/1200942/


All Articles