Choose Language Hide Translation Bar
Community Trekker

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\" );


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

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


  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!

0 Kudos
Staff (Retired)

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.


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

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



x = {"animated.gif", "", "Untitled"};

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