cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
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!

1 REPLY 1
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