As Date
As Date using Files In Directory
Summary: Process of finding and opening the newest file in a specified directory, excluding files with a specific extension.
Code:
Names Default To Here( 1 );
searchext = "jmp"; //extension to look for
searchfolder = "$DESKTOP"; //place to search
lstExtensions = {};
lstDates = {};
lstFiles = Files In Directory( searchfolder );
For Each( {i, j}, lstFiles,
lstExtensions[j] = Char( Right( i, 3 ) );
lstDates[j] = As Date( Creation Date( searchfolder || "/" || Char( i ) ) );
);
dt = New Table( "Files",
New Column( "Name", character, set values( lstFiles ) ),
New Column( "ext", character, set values( lstExtensions ) ),
New Column( "Date", numeric, set values( Matrix( lstDates ) ) )
);
dt << Select where( :ext != searchext );
dt << Delete Rows;
newest = Col Min( :Date );
newfile = searchfolder || "/" || :Name[dt << Get Rows Where( :Date == newest )][1];
Close( dt, no save );
Open( newfile );
Code Explanation:
- Set default names.
- Define file extension.
- Define search folder.
- Initialize extensions list.
- Initialize dates list.
- Get files in directory.
- Loop through each file.
- Extract file extension.
- Get creation date.
- Create table with files.
- Select non-matching extensions.
- Delete non-matching rows.
- Find minimum date.
- Get newest file name.
- Close table without saving.
- Open newest file.