cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
  • New JMP features coming to desktops everywhere this September. Sign up to learn more at jmp.com/launch.
Choose Language Hide Translation Bar
rrtx_mike
Level II

jsl - How to open the most recent file with a specific naming convention?

Hi everyone,

How do I script jsl to open the newest file in a directory that contains a specific naming convention? I have the following code which will open the newest file in the directory:

(

  OpenNewestFile = Files In Directory( "C:\users\me\MyDocuments\" );

  {t,p}={0,1};

  for(i=1, i <= n items(OpenNewestFile),i++,

  ti=Last Modification Date("C:\users\me\MyDocuments\" || OpenNewestFile );

  If(ti>t,t+ti;p=i));

  dt1 = Open("C:\users\me\MyDocuments\"||OpenNewestFile

)

);

Once per day a new file is written into the directory with a name something like "FD_History_2016_08_27_17_36.csv"

How can I modify this script to find the most recent "FD_History_" file and open it while ignoring any other files in the directory?

Thanks in advance!

4 REPLIES 4
Craige_Hales
Super User

Re: jsl - How to open the most recent file with a specific naming convention?

This example uses regex to see if the name ends with .jmp (the $ matches the end of the line) or begins with the letter a (the ^ matches the beginning of the line).  Regex returns a missing value if the match fails.  The RemoveFrom function deletes offending items from the list.  The for loop runs backwards so the deleted items don't change the part of the list that hasn't been examined already.  (When an item is deleted, the indexes of subsequent items change.)

NULL tells regex not to replace the matched text; IGNORECASE tells regex that Jmp and jmp should match.

x=filesindirectory("$desktop");

for(i=nitems(x), i>= 1, i--,

  if( ismissing(regex(x,".Jmp$|^a",NULL,IGNORECASE)), removefrom(x,i) )

);

show(x);

x = {"animated.gif", "stories.jmp", "Untitled 3.jmp"};

You could build the last modification date into this loop, or keep it in a separate loop after this one.

Craige
LargeElk892
Level II

Re: jsl - How to open the most recent file with a specific naming convention?

Hi,

I've a similar issue but in this case the folder name changes per workweek.

The folder structure is:

\\themainfolder\202503\PPT_12345\subfolders\indicators.xlsx.

The mainfolder contains 3 or 4 "YYYYWW" subfolders - e.g. 202503, 202504, 202505 and the number associated with the PPT_12345 folder is random every week.

I managed to get a list of all the YYYYWW subfolders {"202512", "202513", "202514"} but I need to select the newest folder (max number), in this instance "202514". 

Then I need to select only the PPT folder (there are other subfolders but there is only one called "PPT_...").

Could you help me here?

Thank you

Craige_Hales
Super User

Re: jsl - How to open the most recent file with a specific naming convention?

allfiles = Files In Directory( "c:/" );
show(N Items( allfiles )); // 15 or so
dirlist = Filter Each( {f}, allfiles, Is Directory(  "c:/" || f ) );
show(N Items( dirlist )); // 12 or so
sorted = Reverse( Sort List( dirlist ) ); // biggest first
filtered = Filter Each( {f}, sorted, Starts With( f, "Prog" ) );
// {"ProgramData", "Program Files (x86)", "Program Files"}

edit: prepend the path

more: JMP 16 results, above. More recent JMP versions may do a better job with c:/ -- prefiltering file system objects you don't want to see. But the idea is the same, use the JSL functions to keep what you want and filter out the rest. The reversed sort should put the date in position 1.

Craige
LargeElk892
Level II

Re: jsl - How to open the most recent file with a specific naming convention?

Thank you Craige. Sorry for the late response. It works well. The only drawback I've is that the folder contains lots of subfolders and the Files In Directory function takes a long time to run.  Anyway thanks for your help.

Recommended Articles