d12n
d12n

Reputation: 861

Iterate through folder hierarchy

I have a folder (my_files) that has about 1000 folders inside of them. Each of these 1000 folders has 6 csv files inside of them. I would like to get 1000 csv files by aggregating these 6 csv's per directory.

I have the following code:

files<-list.files("/Users/me/Desktop/my_files")
for (i in files)
{
         //open each directory in "files"
        //aggregate all csvs in the directory into one
       //name of the aggregated csvs should be the name of the folder they were inside of
}

I am trying to use something like:

for (i in files)
{
    files2<-list.files("/Users/me/Desktop/my_files/"i)
}

To list the files within the directories in my_files, but obviously that is wrong syntax.

Upvotes: 2

Views: 3155

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

I've created a folder called my_files and populated it with folder1, folder2 and folder3. Each folder holds a file1.txt with a hidden message. Let's see what those messages read. The anonymous function could be adapted to read in all your files and combining them. I will leave you to the task.

# I've created a folder "my_files" that is...
setwd("q:/my_files")

# populated by three subfolders
thousand.folders <- list.dirs(full.names = TRUE)

result <- sapply(thousand.folders[-1], function(x) {
  file <- list.files(x, full.names = TRUE)  
  message(readLines(file))
})

file1 in folder1
file1 in folder2
file1 in folder3

Upvotes: 2

Related Questions