Subscribe Bookmark RSS Feed

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

rrtx_mike

Community Trekker

Joined:

Mar 18, 2016

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
Craige_Hales

Staff

Joined:

Mar 21, 2013

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