?
Solved

VBScript

Posted on 2011-09-14
1
Medium Priority
?
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

770 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