Typically in programming, you would use regular expressions to search for files in a directory that contain a given string, and then use a success or failure expression.
However, in JMP, advanced sorting does not seem to be possible using Files In Directory alone.
Therefore, I am thinking of a process in which I can store the data in a variable, then add conditions to it and scrape it down. What techniques would you use?
If possible, I would like to sort the files using multiple regular expressions.
JMP in use: JMP18.2.1
Filter Each() is the way to go. Example to find .png files that contain "2024-06"
Names Default To Here( 1 );
files = Files In Directory( Pick Directory( "" ) );
filtered_files = Filter Each( {v, i}, files, Contains( v, "2024-06" ) & Ends With( v, ".png" ) );
My name is Ono, and I am a senior tester at the JMP Japan division of SAS Institute Japan.
This has already been answered, but I'll give a simple example just to be sure.
Below, Regex and Filter Each are used to extract strings that start with "E" (^E), contain half-width numbers ([0-9]), and end with ".txt" (\.txt$).
files = {"E001.txt", "B002.txt", "EEEEE.txt", "E001.jpg", "E001.txt.jpg", "C001.txt", "EAB1.txt", "1111.txt"};
filtered_files = Filter Each( {v, i}, files, !Is Missing(Regex(v, "^E(.*)[0-9](.*)\.txt$" )));
The same process can be performed using functions beginning with Pat instead of Regex, but the specification may become a bit longer.
files = {"E001.txt", "B002.txt", "EEEEE.txt", "E001.jpg", "E001.txt.jpg", "C001.txt", "EAB1.txt", "1111.txt"};
filtered_files = Filter Each( {v, i}, files,
Pat Match(v, Pat Pos(0) + "E" + Pat Arb() + Pat Regex("[0-9]") + Pat Arb() + ".txt" + Pat R Pos(0));
);
The return value of Filter Each is also a list, so you can further narrow it down from there.
Below, we further narrow down the results to those that contain the string "001.txt" in the file name.
files = {"E001.txt", "B002.txt", "EEEEE.txt", "E001.jpg", "E001.txt.jpg", "C001.txt", "EAB1.txt", "1111.txt"};
filtered_files = Filter Each( {v, i}, files, !Is Missing(Regex(v, "^E(.*)[0-9](.*)\.txt$" )));
final_list = Filter Each({v,i}, filtered_files, Contains(v,"001.txt"));
Thank you everyone for your thoughtful replies.
Although manipulating variables is very important, I had a hard time understanding it, and after much trial and error I ended up with the code below.
1. Read the path of any folder and create a list of files first
2. Narrow down to CSV only
3. Use regular expressions to filter files that do not contain a specific character
There is one thing I still don't understand: the "Is Missing!" in front of it. What purpose does it serve?
//以下のフォルダにあるファイル一覧を変数に取得する。
files = Files In Directory( "%USERPROFILE%document" );
//絞り込み_CSVファイルである
filtered_files = Filter Each( {v, i}, files, !Is Missing(Regex(v, "^(.*)[0-9](.*)\.csv$" )));
//with_key_とspec_から始まるファイルを除外する;
list = Filter Each( {v, i}, filtered_files, !Is Missing(Regex(v, "^(?!.*with_key_|spec_).*$")));
show(list);
I'm glad we were able to accomplish what you wanted.
An exclamation mark "!" by itself is the "Not logical operator" which negates the expression that follows it. In JMP, the "Not logical operator" reverses the values 1 and 0. (More precisely, in JMP, it returns 1 for 0 and 0 for any non-zero value, including 1).
This is described in the following manual.
(You may find results by searching "JMP Operators Help" in Google search, or "Operator Not" in JMP Help.)
You can also search for "Not" in the JMP pull-down menu under Help > Scripting Index to find results (as described below, the "!" operator has an alias function called Not).
For example, the following inverts a 1 and returns a 0:
Next, we invert the 0 and put 1 back.
If the logical operator "!" is difficult to understand, there is an equivalent function (an alias function) called Not function.
Show(Not(1)); // 「!1」と同じ
Show(Not(0)); // 「!0」と同じ
The Is Missing function returns 1 when the argument is a missing value, and 0 when the argument is a non-missing value. In this example, we want the inverted values of 0 and 1, so we use the Not logical operator "!".
In the following, the argument is a missing value so Is Missing returns 0, but the Not logical operator "!" inverts this to 1.
In the following, Is Missing returns 1 because the argument is a non-missing value, but the "!" inverts it to 0.
Show(!Is Missing("Hello, World!"));
■Digression 1
Note that both the Is Missing function and the Not logical operator ("!") can take matrices and lists as arguments, just like many other functions and operators.
x = [15.3, 12.1, ., 13.2, 14.1, .];
Show(!Is Missing(x));
y = {"りんご", "バナナ", "", "みかん"};
Show(!Is Missing(y));
■ Side note 2
If !Is Missing(x) is confusing, you can also write Is Missing(x)==0 or Is Missing(x)!=1. (The exclamation point followed by an equal sign, "!=", returns 1 if the right-hand side is not equal to the left-hand side, and 0 if they are equal.)
Show(Is Missing("")==0);
Show(Is Missing("")!=1);
Show(Is Missing("Hello, World!")==0);
Show(Is Missing("Hello, World!")!=1);
■Reason for using "!" this time
The Filter Each function extracts only elements for which the evaluation value of the third argument is true (1).
For example, the following extracts only those elements of a list x that are greater than 2:
x = {1,5,2,4,3,2,1,4,5,9,1};
y = Filter Each({v,i},x, v > 2);
Show(y);
If you want to extract elements where v>2 is the opposite, you simply specify v <= 2, but you can also specify !(v > 2).
x = {1,5,2,4,3,2,1,4,5,9,1};
y = Filter Each({v,i},x, !(v > 2));
Show(y);
!(v > 2) returns "1" if v is not greater than 2 (that is, v is less than or equal to 2), and "0" otherwise.
■Additional Notes
I'm not very familiar with regular expressions, so the processing may not necessarily be equivalent, and the code may be harder to read, but is it possible to combine the regular expressions into one as shown below? (I'm sorry if I'm wrong about the processing you want to do.)
files = {
"with_key_001.csv", "spec_001.csv",
"aaa_with_key_001.csv", "aaa_spec_001.csv",
"with_key_001bbb.csv", "spec_001bbb.csv",
"aaa_with_key_001bbb.csv", "aaa_spec_001bbb.csv",
"001.csv", "aaa001.csv", "001bbb.csv", "aaa001bbb.csv",<BR />"001.txt", "aaa001.txt", "001bbb.txt", "aaa001bbb.txt",<BR />"aaa001bbb.csv.txt"
};
list = Filter Each( {v, i}, files, !Is Missing(Regex(v, "^(?!.*with_key_|spec_)(.*)[0-9](.*)\.csv$" )));
show(list);