Solved

VBScript

Posted on 2011-09-14
1
460 Views
Last Modified: 2012-05-12
Experts,

I need a vbscript that will return the file name of the latest date modified file within a folder.

For example 3 files in folder:

test1.xls          4/3/2011
test2.xls          4/1/2009
test3.xls          5/3/2001

I need it to return  test1.xls

Any ideas?

Thanks.
Dim objFSO, filename, strFiles, objFile
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objSelectedFolder = objFSO.GetFolder("c:\temp\")

    Set colFiles = objSelectedFolder.Files
    For Each objFile In colFiles
        strFiles = objFile.name
      end if
    Next
filename =  strFiles

Open in new window

0
Comment
Question by:asmyatt
1 Comment
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
Comment Utility
Hello, you can try this:

Set objFSO = CreateObject("Scripting.FileSystemObject")

SearchFolder = "c:\temp\"
Set objFolder = objFSO.GetFolder(SearchFolder)

NewestFile = ""
NewestDate = #1/1/1970#

For Each objFile In objFolder.Files
    If objFile.DateLastModified > NewestDate Then
        NewestDate = objFile.DateLastModified
        NewestFile = objFile.Name
    End If
Next

WScript.Echo NewestFile

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now