Hi, can someone help me modify this code? I am trying to concatenate csv files per folder. The user will have to select a folder and all the qualified csv folders will be concatenated and be saved according to the folder name. This method makes user pick one folder per script run.
However, what I am trying to do now is to make the user pick the main folder then run the script and concatenate the data per subfolders (saving them individually with its folder name as the filename like in the previous method).
But with these code, all the csv files are being concatenated into one file. Can you help modify and explain how recursive works? And how can I save this files individually (concatenate data per folder) with just opening the main folder. I am using JMP 15. Thank you
p = pick Directory("Select a Folder");
//f = Files In Directory(p);
f = Files In Directory(p, recursive(1));
Dir = Word(-1, p, "/"); //get filename
saveDir = Substr( p, 1, Length( p ) - Length( Word( -1, p, "\/" ) ) - 1 ); //save file outside main folder
/* Removes the file names from the list f which are not CSV files */
for(j=1,j<=N Items(f),j++,
If (Substr(f[j],Length(f[j])-2,3)!= "CSV",
RemoveFrom(f,j,1);
j=j-1;
);
);
A = {}; /* creates a empty list for storing the names of each data table */
B = {};
C = {};
D = {};
k = {};
m = {};
/*Opens each one of the CSV files and deletes all unwanted rows */
for(j=1,j<=N Items(f),j++,
CSV = Open(Concat(p,f[j]), End of Field(comma), Strip Quotes(1), Labels(1),
Column Names Start(3), Data Starts(4), Columns(:MRR=Numeric));
colname = {};
colname = CSV<<get Column Names(String);
if(Contains(colname,"Age")>20 & Contains(colname,"Weight")>50,
InsertInto(A,CSV);
CSV<<Select Where(:Sex =="Female");
CSV<<delete rows;
CSV<<Select Where(:Sex =="");
CSV<<delete rows;,
//Else
InsertInto(B,CSV);
InsertInto(k,f[j]);
);
);
//Concatenating the Group A files
dt1=New Table();
If(N Items(A)>1,
dt2 = dt1<<Concatenate(A, Output Table name("All Data"));
dt2<<Delete Columns("Column 1");
);
Close(dt1, No Save);
//Get column header from the final table
colheader = {};
colheader = dt2<<get Column Names(string);
//Setting the correct column names for the errored files
For(j=1,j<=N Items(B), j++,
If(Ncol(B[j])< N Items(colheader), temp =Ncol(B(j)), temp =N Items(colheader) );
For (i=1,i<=temp,i++,
Column(B[j],i)<<Set name(colheader[i]);
);
);
//Concatenating the Group B files
dt1=New Table();
If(N Items(B)>1,
dt3 = dt1<<Concatenate(B, Output Table name("Remaining Data"));
dt3<<Delete Columns("Column 1");
);
Close(dt1, No Save);
//checking if the column numbers are the same among all the files Group A
flga = 0;
for(j=1, j<=N Items(A),j++,
If ((NCol(A[1]) != NCol(A[j])) & flga == 0,
flga = 1;
);
);
//Closing all the open files
for(j=1, j<=N Items(A),j++,
Close(A[j], No Save);
);
for(j=1, j<=N Items(B),j++,
Close(B[j], No Save);
);
//Showing important messages
If(flga ==1, Dialog("Done!", Button("OK")););
Close(dt2,save(char(saveDir)|| Dir ||".jmp"));