cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMPĀ® Marketplace
Choose Language Hide Translation Bar
CG
CG
Level III

Wildcard selection of characters from a given file name

Hello,

I am trying to look up certain characters from a file name and select those file.

for eg: file name: HELLO123_MT_XYZ,

Code:

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

If( Contains( files[i], "MT" ), //Enter search criteria from file name

Open( files[i] );

)

);

Above code with "MT" will pull all file names with MT as character. But how can i select files with wild card? for eg: *123*XY*

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: Wildcard selection of characters from a given file name

I am confused with your entries.  First, lets clear up the code issue.  I initially made an error in the code.  And then I messed up again. The code you need to be using is:

Contains( files[i], "123" ) > 0 & Contains( files[i], "XY" ) > 0 & Contains( files[i], "XY" ) > Contains( files[i], "123" )

There is nothing magical about this code.  Let me explain it.  The Contains() function looks across a literal string, in your case, the current value of Files[i], looking for the value of the second argument "123".  If found it returns the position it was found in. So the first use of the Contains() makes sure that there is the value "123".  The second use makes sure there is a value of "XY" and the thrid comparison makes sure that "XY" is found after the value of "123".  That is all it is doing.

I apologize for the coding error.....

Jim

View solution in original post

pmroz
Super User

Re: Wildcard selection of characters from a given file name

Regular expressions can provide a more general solution.

 

// Wildcard in a string: *123*XY*

slist = {"HELLO123_MT_XYZ", "HELLO123_MT_Z", "GOODBYE123_MT_XY", "XY_FUBAR_123"};

rx = "(123).*(XY)";

for (i = 1, i <= nitems(slist), i++,
	result = regex(slist[i], rx);
	print(slist[i] || ": " || char(result));

);

View solution in original post

8 REPLIES 8
txnelson
Super User

Re: Wildcard selection of characters from a given file name

If( Contains( files[i], "123" ) > 0 & Contains( files[i], "XY" ) > Contains( files[i], "123" ),
	Open( files[i] )
)
Jim
CG
CG
Level III

Re: Wildcard selection of characters from a given file name

txnelson: Thank you for prompt response.

 

for some reason it still returns all files with "123" and doesn't consider "XY" as a search criteria.

Does it have to do anything with multiple delims?

txnelson
Super User

Re: Wildcard selection of characters from a given file name

oops, my error, it should be:

If( Contains( files[i], "123" ) > 0 & Contains( files[i], "123" ) > 0 & Contains( files[i], "XY" ) > Contains( files[i], "123" ),
	Open( files[i] )
)
Jim
CG
CG
Level III

Re: Wildcard selection of characters from a given file name

Actually if i use below -

If( Contains( files[i], "123" ) > 0 & Contains( files[i], "XY" ) > Contains( files[i], "123"),

I can get result. but one issue i found is, if file name are -

1. HELLOABC_0A_H2ABC

2. HELLOABC_1D_H2ABC

If i select "ABC" and "0A", then i get all files with "ABC" whereas if i use "ABC" and "1D" then i get specific file which contains both characters in the file name.

How does it get differentiate between 0A vs 1D? They both should be treated as string.

CG
CG
Level III

Re: Wildcard selection of characters from a given file name

sorry for multiple posts.

But if i use second criteria as "_0A" then i can get specific file instead when i use "0A" then it ignore this criteria.

I am sure i am missing something very basic condition.

 

Thanks,

Chintan

txnelson
Super User

Re: Wildcard selection of characters from a given file name

I am confused with your entries.  First, lets clear up the code issue.  I initially made an error in the code.  And then I messed up again. The code you need to be using is:

Contains( files[i], "123" ) > 0 & Contains( files[i], "XY" ) > 0 & Contains( files[i], "XY" ) > Contains( files[i], "123" )

There is nothing magical about this code.  Let me explain it.  The Contains() function looks across a literal string, in your case, the current value of Files[i], looking for the value of the second argument "123".  If found it returns the position it was found in. So the first use of the Contains() makes sure that there is the value "123".  The second use makes sure there is a value of "XY" and the thrid comparison makes sure that "XY" is found after the value of "123".  That is all it is doing.

I apologize for the coding error.....

Jim
pmroz
Super User

Re: Wildcard selection of characters from a given file name

Regular expressions can provide a more general solution.

 

// Wildcard in a string: *123*XY*

slist = {"HELLO123_MT_XYZ", "HELLO123_MT_Z", "GOODBYE123_MT_XY", "XY_FUBAR_123"};

rx = "(123).*(XY)";

for (i = 1, i <= nitems(slist), i++,
	result = regex(slist[i], rx);
	print(slist[i] || ": " || char(result));

);
CG
CG
Level III

Re: Wildcard selection of characters from a given file name

Thank you for responses.

I didn't notice i had same character repeating twice in file name and hence it was pulling whole bunch of file.

I limited string length of file name and now i can get files based on search criteria.

 

Thank you very much guys!