R Select files in working directory based on criteria in CSV

Viewed 91

I have a working directory called "NVSPL" of over 7,000 .txt files. Some of those files I want to ignore during my analyses. I've created a separate CSV file with a column that contains the name and extension of each of the over 7,000 .txt files in my working directory. The second column in the CSV has either a "0" if I want to keep the associated .txt file or a "1" if I want to ignore the associated .txt file.

How can I have R go through the CSV, find all of the "0"s and create a new folder with the associated .txt files?

enter image description here

In this case, I would want a new folder that would only include files with "0"s in the "GoodData" column.

Thank you for your time and effort.

1 Answers

We can use file.copy. Subset the files from the 'FileName' column and then with file.copy copy those files into a new directory created

files <- file.path("path/to", with(df1, FileName[!GoodData]))
dir.create("path/to/NVSPL_new", recursive = TRUE)


for(file in files) {

   file.copy(file, "path/to/NVSPL_new")
   }
Related