cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar

Concatenating large number of files

Hi Guys,

I am trying to concatenate several files in a folder as below:

11 REPLIES 11
mattf
Level V

Re: Concatenating large number of files

See:
Jump into JMP Scripting
By: Wendy Murphrey and Rosemary Lucas
// http://www.sas.com/apps/pubscat/bookdetails.jsp?catid=1&pc=61733
Page 122, Soultion 5.16

Set Current Directory( "C:\temp2" );
myFiles = Files In Directory( "C:\temp2" );
For( i = 1, i <= N Items( myFiles ), i++,
/* If the file is the first in the list, open it. Otherwise, open the table, concatenate with the main table, and close the table just opened. */
If( i == 1,
mainDt = Open( "C:\temp2\" || myFiles ),
dt = Open( "C:\temp2\" || myFiles );
mainDt = mainDt << Concatenate( dt, Append to First Table );
Close( dt, NoSave );
Wait( 0 );
)
);
/* Give the final table a name. */
mainDt << Set Name( "Concatenated Files" );


Best regards,
-Matt
mattf
Level V

Re: Concatenating large number of files

See:
Jump into JMP Scripting
By: Wendy Murphrey and Rosemary Lucas
// http://www.sas.com/apps/pubscat/bookdetails.jsp?catid=1&pc=61733
Page 122, Soultion 5.16

Set Current Directory( "C:\temp2" );
myFiles = Files In Directory( "C:\temp2" );
For( i = 1, i <= N Items( myFiles ), i++,
/* If the file is the first in the list, open it. Otherwise, open the table, concatenate with the main table, and close the table just opened. */
If( i == 1,
mainDt = Open( "C:\temp2\" || myFiles ),
dt = Open( "C:\temp2\" || myFiles );
mainDt = mainDt << Concatenate( dt, Append to First Table );
Close( dt, NoSave );
Wait( 0 );
)
);
/* Give the final table a name. */
mainDt << Set Name( "Concatenated Files" );

Re: Concatenating large number of files

Thanks. Can you please use the following to post your code?

mpb
mpb
Level VII

Re: Concatenating large number of files

You may find this trick useful:

After logging in, "reply" to the posting and "Quote Original". You will the have the complete code which you can copy / paste into a JMP script window. Then remove the first 2 characters on each line ("> ") to get the code as it was intended. Then cancel the reply.

Re: Concatenating large number of files

Thanks Matt, that worked. I will take a look at the book.

Thanks, mpb for the tip.
mattf
Level V

Re: Concatenating large number of files

 
jorgeramosdealm
Level III

Re: Concatenating large number of files

I made a variation of that code that incorporates the Source table name for each file. In this case it takes all the txt files but you can also do it with csv or other format.


mypath ="C:\temp\";


Set Current Directory( mypath ); 


y = Files In Directory( mypath ); 


For( i = N Items( y ), i > 0, i--,


  If( Ends With( y[i], "txt" ),


  ,


  Remove From( y, i )


  )


);


n = N Items( y );


For( i = 1, i <= N Items( y ), i++,


  sname=left(y[i],length(y[i])-4);


/* If the file is the first in the list, open it. Otherwise, open the table, concatenate with the main table, and close the table just opened. */ 


    If( i == 1,


        mainDt = Open( mypath || y[i] );


        mainDt << New Column ("source", character, set each value ( sname )),


        dt = Open( mypath || y[i] ) ;


        dt << New Column ("source", character, set each value ( sname ));


        mainDt = mainDt << Concatenate( dt, Append to First Table );


        Close( dt, NoSave );


        Wait( 0 );


    )


  );   


/* Give the final table a name. */ 


mainDt<< Set Name("Concatenated Files");


mjvincent87
Level II

Re: Concatenating large number of files

I am hoping to find out where I am going wrong with this one.  The debugger won't start but I have about 20,000 files, could that be the problem?

 

Set Current Directory( mypath );  


y = Files In Directory( mypath );  


For( i = N Items( y ), i > 0, i--, 


  If( Ends With( y[i], "csv" ), 


  , 


  Remove From( y, i ) 


  ) 


); 


n = N Items( y ); 


For( i = 1, i <= N Items( y ), i++, 


  sname=left(y[i],length(y[i])-4); 


/* If the file is the first in the list, open it. Otherwise, open the table, concatenate with the main table, and close the table just opened. */  


    If( i == 1, 


        mainDt = Open( mypath || y[i] ); 


        mainDt << New Column ("source", character, set each value ( sname )), 


        dt = Open( mypath || y[i] ) ; 


        dt << New Column ("source", character, set each value ( sname )); 


        mainDt = mainDt << Concatenate( dt, Append to First Table ); 


        Close( dt, NoSave ); 


        Wait( 0 ); 


    ) 


  );    


/* Give the final table a name. */  


mainDt<< Set Name("Terminal");
stan_koprowski
Community Manager Community Manager

Re: Concatenating large number of files

hi @mjvincent87 

The original post is several years old and some better options exist for importing multiple files from a directory.

 

Have you seen the multiple file import that was introduced in JMP 14?

The new multiple file import platform launches a dialog to load several files into a single JMP data table. In addition, it allows filtering of files by size, date, name and type and supports one-column import, useful for Text Explorer document preparation. Also the platform offers support for editing text import options.

 

cheers,

Stan