Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
Stokes
Level III

[Query] For loop to import txt file and close txt file one by one

Hi, can someone teach me how to run For loop to,

import txt file -->

run a formula -->

save the file in the same folder -->

close the file -->

find next txt file

 

I have many files, so can't open all of them to process the formula.

Thanks in advance.

3 REPLIES 3
Highlighted
Georg
Level IV

Re: [Query] For loop to import txt file and close txt file one by one

This example May help to find the solution:

// Initialization
Names Default To Here( 1 );
clear log();

// prepare source and destination directory
show(get path variable("SAMPLE_IMPORT_DATA"));
show(get path variable("TEMP"));

basepath_str = "$SAMPLE_IMPORT_DATA";
resultpath_str = "$TEMP" || "\jmp\";

// create folder for destination if not existing
If( Directory Exists( resultpath_str ),
	,
	Create Directory( resultpath_str )
);

// read filelist
f_lst = Files In Directory( basepath_str );

// go through all csv files, open, add formula and save
dt_lst = {};
For( i = 1, i <= N Items( f_lst ), i++,
	If( Ends With( f_lst[i], "csv" ),
		cdt = Open( basepath_str ||"\"|| f_lst[i] );
		Insert Into( dt_lst, cdt );
		Show( cdt );
		cdt << New Column( "Formulat_row", Formula( Row() ) );
		cdt_fullfilename = resultpath_str || (cdt << get name) || ".jmp";
//		Close( cdt, save( resultpath_str || (cdt << get name) || ".jmp") );
	)
);
Georg
Highlighted

Re: [Query] For loop to import txt file and close txt file one by one

Hi,

 

Just to add to this response, and you seen the Multiple File Import feature? 

https://www.jmp.com/support/help/en/15.1/?os=win&source=application&utm_source=helpmenu&utm_medium=a...

Highlighted
Stokes
Level III

Re: [Query] For loop to import txt file and close txt file one by one

Thanks both of you guys help, I appreciate it.