cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Don’t miss your chance to experience Discovery Summit Europe at our best available rate! Early bird registration through 31 Oct.
  • Image to Table JMP extension imports single or batch images as data tables. Useful to extract curves from pictures or convert image data to curve data to use in analyses like Functional Data Explorer. Download and install the extension.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
UserID16644
Level V

Open files using contains ()

Hi all, 

I am try to create a script that would go through directories to find the input file. However, some file names may have different characters. 

 

For example:

My input in favorite fruit would be: Orange

Existing files from folder would be: Mark_Orange_Mango.jmp, Jane_Apple.jmp, John_Orange.jmp

Files that would open: Mark_Orange_Mango.jmp and John_Orange.jmp

 

How can I open these files using contains() or what function do I need to use? I am using JMP 15

 

Here is the piece of code I am working on:

 

nw = New Window( "Favorite Fruits", << modal(),
	lineupbox(ncol(2), spacing(5),
		
		Text Box( "Name:" ),
		teb1 = Text Edit Box( "", <<set width( 150 ) ),
		
		Text Box( "Age:" ),
		teb2 = Text Edit Box( "", <<set width( 150 ) ),

		Text Box( "Birthday:" ),
		teb3 = Text Edit Box( "", <<set width( 150 ) ),
		
		Text Box( "Favorite Fruit:" ),
		teb4 = Text Edit Box( "", <<set width( 150 ) ),
	),
	Button Box( "OK", 
		name = teb1 << get text ();
		age = teb2 << get text ();
		bday = teb3 << get text ();
		fruit = teb4 << get text ();	
	),
);


/* ----- opening the files ----- */

path = "C:\Users\Desktop" || Char (name) || "\" || Char (bday);

files = Files in Directory (path);

For (j=1, j<= N Items (files), j++,
	If (Contains (fruit[j], ".jmp"), // Open file that contains the input from text box
	...
	)
);
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Open files using contains ()

See if this works for you

nw = New Window( "Favorite Fruits",
	<<modal(),
	Lineup Box( N Col( 2 ), spacing( 5 ), 
		
		Text Box( "Name:" ),
		teb1 = Text Edit Box( "", <<set width( 150 ) ), 
		
		Text Box( "Age:" ),
		teb2 = Text Edit Box( "", <<set width( 150 ) ), 

		Text Box( "Birthday:" ),
		teb3 = Text Edit Box( "", <<set width( 150 ) ), 
		
		Text Box( "Favorite Fruit:" ),
		teb4 = Text Edit Box( "", <<set width( 150 ) ),

	),
	Button Box( "OK",
		name = teb1 << get text();
		age = teb2 << get text();
		bday = teb3 << get text();
		fruit = teb4 << get text();
	),

);

/* ----- opening the files ----- */

path = "C:\Users\Desktop" || Char( name ) || "\" || Char( bday );

files = Files In Directory();

For( i = N Items( files ), i >= 1, i--,
	If( Word( -1, files[i], "." ) != "jmp",
		Remove From( files, i, 1 )
	)
);

If( N Items( Files ) > 0,
	tableList = {};
	For( j = 1, j <= N Items( files ), j++,
		If( Contains( files[j], fruit ) > 0, // Open file that contains the input from text box
			dd = Open( path || "/" || files[j] );
			Insert Into( tableList, dd );
		)
	);
);
Jim

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: Open files using contains ()

See if this works for you

nw = New Window( "Favorite Fruits",
	<<modal(),
	Lineup Box( N Col( 2 ), spacing( 5 ), 
		
		Text Box( "Name:" ),
		teb1 = Text Edit Box( "", <<set width( 150 ) ), 
		
		Text Box( "Age:" ),
		teb2 = Text Edit Box( "", <<set width( 150 ) ), 

		Text Box( "Birthday:" ),
		teb3 = Text Edit Box( "", <<set width( 150 ) ), 
		
		Text Box( "Favorite Fruit:" ),
		teb4 = Text Edit Box( "", <<set width( 150 ) ),

	),
	Button Box( "OK",
		name = teb1 << get text();
		age = teb2 << get text();
		bday = teb3 << get text();
		fruit = teb4 << get text();
	),

);

/* ----- opening the files ----- */

path = "C:\Users\Desktop" || Char( name ) || "\" || Char( bday );

files = Files In Directory();

For( i = N Items( files ), i >= 1, i--,
	If( Word( -1, files[i], "." ) != "jmp",
		Remove From( files, i, 1 )
	)
);

If( N Items( Files ) > 0,
	tableList = {};
	For( j = 1, j <= N Items( files ), j++,
		If( Contains( files[j], fruit ) > 0, // Open file that contains the input from text box
			dd = Open( path || "/" || files[j] );
			Insert Into( tableList, dd );
		)
	);
);
Jim
UserID16644
Level V

Re: Open files using contains ()

Hi, it is working, however, is it possible to have multiple conditions on my contain() function?

txnelson
Super User

Re: Open files using contains ()

It can be as complex as you want it to be

If(
(Contains( files[j], fruit ) & Contains( files[j], "Good Luck" )) |
Contains( files[j], "Special" ),
dd = Open( path || "/" || files[j] )
)

I strongly suggest you take some time to read the Scripting Guide.  It will make your journey into scripting far more pleasent. 

Jim

Recommended Articles