Get Latest File

I was helping out in the MSN Groups scripting forum recently. The admin needed to find the latest file or most recently modified file in a folder. That sounded reasonable to me so I put together a function and short script that uses it. I thought I’d share it here:

The real “trick” to the script is to convert the DateLastModified property to a Date using CDate. Otherwise, the script tries to compare strings and you get some pretty weird and unexpected results. As written, this function will not recurse through subdirectories. Although you could make that modification.  It also searches all file types.  If you wanted to be more restrictive, say find the last Word document, you could either parse out the extension from the filename and only check .DOC or you could check the Type property of the file. This property will be something like “Microsoft Office Word 97 – 2003 Document”.

You could also modify this function to return all files that had been modified since a certain date. Here’s one possible modification:

 

Now you’ll get a comma separated list of all files that have been modified since 2/15/2007.

So kick it around. If you come up with some tweaks, post a comment.

Technorati tags: , , FileSystemObject,

2 thoughts on “Get Latest File

  1. So many things like this are much easier in PowerShell. Which in fact was my first inclination. But since he had a larger VBScript he was working on that’s what I had to work with. But it is very apparent that you can get much more done with less code in PowerShell than VBScript.

    Thanks for the tip.

Comments are closed.