0

UPDATE

Thanks for the suggestions. This is how for I got so far, but I still don't find how I can get the loop to work within the file path name.

setwd("//tsclient/C/Users/xxx") 
folders <- list.files("TEST")

--> This gives me a list of my folder names

for(f in folders){
setwd("//tsclient/C/xxx/[f]") 

files[f] <- list.files("//tsclient/C/Users/xxx/TEST/[f]", pattern="*.TXT")
mergedfile[f] <- do.call(rbind, lapply(files[f], read.table))

write.table(mergedfile[f], "//tsclient/C/Users/xxx/[f].txt", sep="\t")
}

I have around 100 folders, each containing multiple txt files. I want to create 1 merged file per folder and save that elsewhere. However, I do not want to manually adapt the folder name in my code for each folder.

I created the following code to load in all files from a single folder (which works) and merge these files.

setwd("//tsclient/C/xxx") 

files <- list.files("//tsclient/C/Users/foldername", pattern="*.TXT")
        file.list <- lapply(files, read.table)
        setattr(file.list, "names", files)
        masterfilesales <- rbindlist(file.list, idcol="id")[, id :=  substr(id,1,4)]

write.table(masterfilesales, "//tsclient/C/Users/xxx/datasets/foldername.txt", sep="\t")

If I wanted to do this manually, I would every time have to adapt "foldername". The foldernames contain numeric values, containing 100 numbers between 2500 and 5000 (always 4 digits).

I looked into repeat loops, but those don't run using it within a file path. If anyone could direct me in a good direction, I would be very grateful.

research111
  • 347
  • 5
  • 18
  • 3
    Use either `list.files` with include.dirs = TRUE or `list.dirs` to create a character vector and then `paste` to whatever path is at top of this tree. Surely this has been asked on SO before. What search strategy did you use? Here is my candidate for a duplicate: http://stackoverflow.com/questions/34345062/copy-multiple-files-from-multiple-folders-to-a-single-folder-using-r – IRTFM Dec 22 '15 at 21:56
  • Thank you. That post does allow me to create a character vector of folder names. `folders <- list.files("Directory")` `files <- sapply(folders, list.files, full.names = TRUE)` I don't simply want to copy files, but repeat the above merging procedure separately for each folder within my director. I don't see yet how i integreate this with the above procedure – research111 Dec 22 '15 at 22:26
  • Turn your merging procedure into a function of a filepath. – Gregor Thomas Dec 22 '15 at 22:48
  • I'm pretty sure there are question out there for merging multiple files as well. One of hte Cpursera classes has such a problem and students often bring their homework to SO (on dubious moral basis.) – IRTFM Dec 23 '15 at 01:43

0 Answers0