Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Latest File

Posted on March 2, 2007July 2, 2013

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:

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

'GetLatestFile.vbs
strDir="S:"
WScript.Echo "The newest file is " & GetLatestFile(strDir)

Function GetLatestFile(strDir)
'Returns the full path and file name of the file that
'was most recently modified. This function will Not
'recurse through subdirectories.

On Error Resume Next
Set objFSO=CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDir) Then

Set objFldr=objFSO.GetFolder(strDir)
Set colFiles=objFldr.Files

strLatest=""
dtLatest="01/01/1900 12:00:00 AM"

For Each file In colFiles
If CDate(file.DateLastModified) > CDate(dtLatest) Then
strLatest=file.path
dtLatest=file.DateLastModified
End If
Next

GetLatestFile=strLatest
Else
GetLatestFile="Directory Not Found"
End If

End Function
 

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:

 

dtCutOff="02/15/2007 12:00:00 AM"

For Each file In colFiles
If CDate(file.DateLastModified) > CDate(dtCutoff) Then
strLatest=strLatest & "," & file.path
End If
Next

'strip off leading comma
strLatest=Mid(strLatest,2)
GetLatestFile=strLatest

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.


Behind the PowerShell Pipeline
Technorati tags: VBScript, Scripting, FileSystemObject, Functions

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Get Latest File”

  1. Vinicius Canto says:
    March 2, 2007 at 10:46 pm

    Powershell Version:

    (ls | sort creationtime -desc)[0]

    Thank you!

    —
    Vinicius Canto
    MVP Visual Developer – Scripting
    MCP Windows 2000 Server, Windows XP e SQL Server 2000
    Blog sobre scripting: http://viniciuscanto.blogspot.com

  2. Jeffery Hicks says:
    March 3, 2007 at 10:40 am

    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.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d