Wildcard selection of characters from a given file nameHello,
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*
Re: Wildcard selection of characters from a given file nameThank 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!
Re: Wildcard selection of characters from a given file nameRegular 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));
);Re: Wildcard selection of characters from a given file nameI 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:
<SPAN class="token function">Contains</SPAN><SPAN class="token punctuation">(</SPAN> files<SPAN class="token punctuation">[</SPAN>i<SPAN class="token punctuation">]</SPAN><SPAN class="token punctuation">,</SPAN> <SPAN class="token string">"123"</SPAN> <SPAN class="token punctuation">)</SPAN> <SPAN class="token operator">></SPAN> <SPAN class="token number">0</SPAN> <SPAN class="token operator">&</SPAN> <SPAN class="token function">Contains</SPAN><SPAN class="token punctuation">(</SPAN> files<SPAN class="token punctuation">[</SPAN>i<SPAN class="token punctuation">]</SPAN><SPAN class="token punctuation">,</SPAN> <SPAN class="token string">"XY"</SPAN> <SPAN class="token punctuation">)</SPAN> <SPAN class="token operator">></SPAN> <SPAN class="token number">0</SPAN> <SPAN class="token operator">&</SPAN> <SPAN class="token function">Contains</SPAN><SPAN class="token punctuation">(</SPAN> files<SPAN class="token punctuation">[</SPAN>i<SPAN class="token punctuation">]</SPAN><SPAN class="token punctuation">,</SPAN> <SPAN class="token string">"XY"</SPAN> <SPAN class="token punctuation">)</SPAN> <SPAN class="token operator">></SPAN> <SPAN class="token function">Contains</SPAN><SPAN class="token punctuation">(</SPAN> files<SPAN class="token punctuation">[</SPAN>i<SPAN class="token punctuation">]</SPAN><SPAN class="token punctuation">,</SPAN> <SPAN class="token string">"123"</SPAN> <SPAN class="token punctuation">)</SPAN>
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.....
Re: Wildcard selection of characters from a given file namesorry 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
Re: Wildcard selection of characters from a given file nameActually 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.
Re: Wildcard selection of characters from a given file nameoops, 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] )
)
Re: Wildcard selection of characters from a given file nametxnelson: 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?
Re: Wildcard selection of characters from a given file nameIf( Contains( files[i], "123" ) > 0 & Contains( files[i], "XY" ) > Contains( files[i], "123" ),
Open( files[i] )
)