1

So I'm trying to create a program that searches for the most recently updated file in a directory. My initial hope was that a command like

file_array = FILE_INFO(file_path+'\*.dat')

would create an array of all of the files in the directory, and then

edit_time = file_array.mtime

would give me an array of all of the mtimes, from which I could get the max mtime, aka the most recently updated file. As far as I can tell, though, FILE_INFO (and FSTAT) does not seem to be able to handle multiple files at the same time.

This program is supposed to be an automated procedure, and files are constantly being updated and added as data is pushed onto the computer. So hard coding in anything more specific than the parent directory is not a viable solution.

So what I need is an alternative to FILE_INFO that can handle multiple files, or a loop procedure that can step through the directory, looking at every file, without first knowing the file names.

1 Answers1

0

How about something like this:

function mg_newest_file, dirname, _extra=e
  compile_opt strictarr

  files = file_search(filepath('*', root=dirname), count=nfiles, _extra=e)
  info = replicate(file_info(files[0]), nfiles)
  for i = 0L, nfiles - 1L do begin
    info[i] = file_info(files[i])
  endfor

  ind = sort(info.mtime)
  return, files[ind[-1]]
end

Call it like this to find the newest regular file in a given directory:

IDL> print, mg_newest_file('/Users/mgalloy/projects/mglib', /test_regular)
/Users/mgalloy/projects/mglib/homebrew_configure.sh

Depending on what you are doing, FOLDERWATCH (introduced in IDL 8.4) might be useful to you.

mgalloy
  • 2,356
  • 1
  • 12
  • 10
  • I'm restricted to using 8.2 because of the licensing we have, so folderwatch won't be possible. I'll try out your solution and get back to you, though. – Brendan Battey Aug 12 '15 at 19:01