I want to use R's list.files function to find all text files in a folder and in its subfolders. However, I would like to exclude all files that are in one subfolder, let's say it contains unfinished work which is not ready for the things I use the other files for. Structure is like this:
- folder
|- subfolder_1_good_stuff
|- subfolder_2_good_stuff
|- subfolder_3_good_stuff
|- subfolder_4_unfinished_stuff
So "folder" would be my working directory.
I would now use:
list.files(path=".", pattern=".txt", recursive=TRUE)
But what should I add to "path" expression in order to exclude folder with unfinished stuff. This folder name will not be present in any filenames, if that makes some difference.
Under Virus & threat protection settings, select Manage settings, and then under Exclusions, select Add or remove exclusions. Select Add an exclusion, and then select from files, folders, file types, or process. A folder exclusion will apply to all subfolders within the folder as well.
Adding -ls or -exec ls -l {} \; would make it like ls -l without directories.
To exclude multiple directories, OR them between parentheses. And, to exclude directories with a specific name at any level, use the -name primary instead of -path .
List Files Using “Grep” Command: First, we will use the grep command within the “ls” list command to list all the files residing in the particular folder. Try the below “ls” command along with the “-la” flag to list all the regular files, e.g., hidden or not.
Building upon @zx8754's answer, just with tidyverse
approach using %>%
:
library(tidyverse)
list.files(path=".", pattern=".txt", full.names = TRUE, recursive=TRUE) %>%
stringr::str_subset(., "unfinished_stuff", negate = TRUE)
Use regex - grepl
to exclude:
# find all ".txt" files
myfiles <- list.files(path = ".", pattern = ".txt",
full.names = TRUE, recursive = TRUE)
# exclude unfinished stuff
myfilesfinished <- myfiles[ !grepl("unfinished_stuff", myfiles) ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With