HP ALM VBScript - Upload the Most Recent File in Folder

Some test artifacts are generated with random names or appended with time-stamps.

This makes it difficult to attach the right file to the test run.

Below is an example to find the latest screenshot file.

REM Upload test screenshot.
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(screenshotPath)
Set recentFile = Nothing
REM Search for the latest image file in the folder.
For Each file in folder.Files
   If UCase(fso.GetExtensionName(file.name)) = "PNG" Then
      If (recentFile is Nothing) Then
        Set recentFile = file
      ElseIf (file.DateLastModified > recentFile.DateLastModified) Then
        Set recentFile = file
      End If
   End If
REM Upload recent image file.
TDHelper.UploadAttachment screenshotPath & "\" & recentFile.name, CurrentRun

The loop looks at each file in the folder and compares the Date Last Modified.

The recentFile variable is updated only if the file compared is later.