Solved

VBScript

Posted on 2011-09-14
1
465 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 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 36539654
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
which modules are active in VB6 project? 6 40
MS SQL Update query with connected table data 3 36
backup program with robocopy 6 43
Button function on table is in trouble 3 20
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 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