# How to use 'Join' and '& /@' : combining all .csv files in a directory into a single .csv file automatically

I would like to import all .csv files in a directory and combine them into a single file using Join. I can do this manually but when I try to automate it using &/@ I run into problems.

The datafiles and example Mathematica notebook described in this question is available at this google drive link

Details

There are 7 files in the notebook directory, each with 6 header rows and ten rows of data. (Each row has 8 columns).

I use the code below to do the following:

1. Search a directory for all file names containing ".csv"
2. Import them
3. Remove the header from each file.
root = NotebookDirectory[];

filenames = FileNames["*.csv", root];
files = Import[#] & /@ filenames;
files2 = Drop[#, headerROWS] & /@ files;


I verify that everything imported corrected as shown in screenshot below (7 files, each with 10 rows of data).

Stupid Manual Solution Using the same placeholder # and Join, it's straightforward but annoying to accomplish what I want manually:

MANUALjoin =
Join[files2[[1]], files2[[2]], files2[[3]], files2[[4]], files2[[5]],
files2[[6]], files2[[7]]]; (*the dumb, manual way I want to avoid*)



The screenshot below shows this working as expected, i.e.: it simply concatenates the files while retaining the structure of the individual files.

The Problem When I try to automate the Join operation using code analogous to the &/@ in the first block of code, it jumbles the placement of the rows. This is what I've tried.

AUTOjoin = Join[#] & /@ files2;  (*Fails *)


I see the following jumbled output:

It seems that [#] is somehow incompatible with Join but I don't see exactly how. It seems that this code should be identical to the 'manual' version above. Does someone see what the problem is exactly?

• Try Join @@ files2. Mar 5, 2021 at 22:58

Join is expecting a sequence, as you can see in your MANUALjoin. You can convert your list of lists into a sequence of lists as follows:

SetDirectory[NotebookDirectory[]];

Incidentally, Join[#] & /@ files2 is equivalent to {Join[files2[[1]]],Join[files2[[2]]],...}, not Join[files2[[1]],files2[[2]],...]
• We could also write autojoin = Catenate[files2];. Mar 6, 2021 at 0:58
• @WReach, yes this is a better solution. Also Flatten[files2, 1] Mar 6, 2021 at 2:49