cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
Craige_Hales
Super User
MFI Update CSV

@Helal  asked in Import Multiple CSV Files from different folder, Update Value, and save CSV with different names  how to do several things: gather a bunch of CSV files from a directory tree, make edits, and save the files back to their original directory, as CSV, renamed slightly. This JSL will get you started.

Do be careful with the Delete Directory! Don't use it in your code!

// JMP 15 or later required; JMP 14 does not have the recursive MFI option

dir = Convert File Path( "$temp/deletemeCSV/" ); // keep the paths in variables
// Delete Directory( dir ); // CAREFUL!!!!!
Create Directory( dir ); // making dummy data
subdira = Convert File Path( dir || "argyle/" ); // dummy sub dirs
subdirb = Convert File Path( dir || "plaid/" );
Create Directory( subdira );
Create Directory( subdirb );
dt = Open( "$sample_data/big class.jmp" ); // use this for dummy data
dt:name[1] = "katieA"; // make the dummy files distinct
dt << save( subdira || "a.csv" );
dt:name[1] = "katieB";
dt << save( subdirb || "b.csv" );
Close( dt, nosave ); // done with big class

dtlist = // MFI reurns a { list } of data tables, even if only one
Multiple File Import( // I removed a lot of default parameters
	<<Set Folder( dir ),
	<<Set Subfolders( 1 ), // the recursive parameter
	<<Set Stack Mode( "Table Per File" ) // separate tables
) << Import Data;

For( i = 1, i <= N Items( dtlist ), i += 1, // look at each returned table
	dt = dtlist[i]; // in the list of tables from MFI
	For Each Row(
		dt, // do your edits on the tables here...
		If( dt:name == "katieA",
			dt:age = 98
		);
		If( dt:name == "katieB",
			dt:age = 99
		);
	);
	// there is a files property in the table that has the source file partial path
	sourceFile = (dt << getproperty( "Files" ))[1][1];
	// replace the .csv of the original name with _new.csv
	destfile = Substr( sourcefile, 1, Length( sourcefile ) - 4 ) || "_new.csv";
	// put the full path in front of the partial path and save beside the original
	dt << save( dir || destfile );
	Close( dt, nosave ); // done with that file, it is now saved as a csv
);

// there should be 4 files now
Show( Files In Directory( dir, recursive( 1 ) ) );

// peek at the new ones
Open( subdira || "a_new.csv" );// age 98
Open( subdirb || "b_new.csv" );// age 99
// show the first two lines of csv text in the log
Write( "\!n\!n", Left( Load Text File( subdira || "a_new.csv" ), 47 ) );// age 98
Write( "\!n\!n", Left( Load Text File( subdirb || "b_new.csv" ), 47 ) );// age 99

 

Last Modified: Nov 8, 2021 8:23 PM