How to find last modified file (TestComplete, QTP, VBScript)

Categories: File System OperationsHow toSource code

Reference page: Service Functions – System (QTP, VBScript)

Description

Check folder exists - check folder has files. Iterate through collection of files and keep reference of the most recently modified file (its DateLastModified property is always the highest number). The remaining reference is the one that you need. In case of errors the function returns Nothing.

Note. In some cases, you may want to use DateCreated or DateLastAccessed properties.

Implementation

Public Function GetLastModifiedFile(ByVal sFolderPath)
 Dim FSO, objFolder, objFile
 Dim objFileResult, longDateTime
 Dim boolRC
 Set FSO = CreateObject("Scripting.FileSystemObject")
 boolRC = FSO.FolderExists(sFolderPath)
 If Not boolRC Then
 Set FSO = Nothing
 Set GetLastModifiedFile = Nothing
 Exit Function
 End If
 Set objFolder = FSO.GetFolder(sFolderPath)
 If objFolder.Files.Count = 0 Then
 Set FSO = Nothing
 Set objFolder = Nothing
 Set GetLastModifiedFile = Nothing
 Exit Function
 End If
 Set objFileResult = Nothing
 longDateTime = CDate(0)
 For Each objFile in objFolder.Files
 If objFile.DateLastModified > longDateTime Then
 Set objFileResult = objFile
 longDateTime = objFile.DateLastModified
 End If
 Next
 Set FSO = Nothing
 Set objFolder = Nothing
 Set GetLastModifiedFile = objFileResult
End Function

  • 3 responses to "How to find last modified file (TestComplete, QTP, VBScript)"

  • pavan
    18th January 2012 at 12:23

    Thanks. it’s useful website…..

  • Matt
    25th April 2012 at 6:46

    Hi,

    Do you know the script to get last modified file from a WebTable?

    Cheers,
    Matt

    [Author's reply.
    Hi, Matt..

    I don’t “know” scripts, I develop them - based on context.
    If one can’t clearly understand a context, there’s a high chance of implementing invalid solution.
    Your question is clearly lack of context. You wanna help me out here?

    -Author]

  • teju
    9th July 2013 at 21:43

    Hi,How can i get the latest file from the folder.

    file names will be as below.
    DSINLOAD.J44006
    DSINLOAD.J44007
    DSINLOAD.J44008

    but created on the same day with diff time…and file names start with DSINLOAD it can end up with any extension.

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by the author is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.