locked
Trying to compare the 2 most recent files in a folder and process if different RRS feed

  • Question

  • Using VBScript, I need to compare the 2 most recent files in a folder, and if they are different, set an error level that I can pass to the calling program. New files will be transferred every 10 minutes and as part of the process I need to compare the files so the user can process the new data if it exists. At the start of the day, it will compare between the new file and a static blank file to see if data has been added. The file names will be in the format filename-mmddyyyy-hhmmssss.csv


    So File0 (static) is a blank csv file

    File1 (first file of the day) has no data, so no error level set and my calling program can complete it's loop

    File2 (second file of the day) has data in it, so it's different from File1, so I want to set an error level 10 indicating the files are different, and copy the newest file to a separate location with a fixed file name for further processing.  The calling program will then do it's part to email the user that a file needs to be processed.

    and so on throughout the day.

    I'm OK in following a vbscript, but lousy at creating one from scratch. 

    TIA for any help you can offer!

    Edit:

    Here is what I have so far, but not getting the output expected.

    'Delete files from previous run
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objStartFolder = "L:\Inbox\Test\"

    Set objFolder = objFSO.GetFolder(objStartFolder)

    Set colFiles = objFolder.Files
    For Each objFile in colFiles
       if instr(objFile.Name,".csv") then
           objFSO.DeleteFile "L:\Inbox\Test\*.*"
       end if
    Next


    'Copy the newest 2 files to the tesing folder
    src = "L:\Inbox"
    dst = "L:\Inbox\Test"

    Set fso = CreateObject("Scripting.FileSystemObject")

    mostRecent = Array(Nothing, Nothing)

    For Each f In fso.GetFolder(src).Files
      If LCase(fso.GetExtensionName(f.Name)) = "csv" Then
        If mostRecent(0) Is Nothing Then
          Set mostRecent(0) = f
        ElseIf f.DateLastModified > mostRecent(0).DateLastModified Then
          Set mostRecent(1) = mostRecent(0)
          Set mostRecent(0) = f
        ElseIf mostRecent(1) Is Nothing Or f.DateLastModified > mostRecent(1).DateLastModified Then
          Set mostRecent(1) = f
        End If
      End If
    Next

    For i = 0 To 1
      If Not mostRecent(i) Is Nothing Then mostRecent(i).Copy dst & "\"
    Next

    'Compare the 2 files in  L:\Inbox\Test
    set f1 = mostRecent(0)
    set f2 = mostRecent(1)
    WScript.Echo f1
    WScript.Echo f2

    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile1 = objFSO.OpenTextFile(f1, ForReading)
    strCurrentDevices = objFile1.ReadAll
    objFile1.Close
    Set objFile2 = objFSO.OpenTextFile(f2, ForReading)
    Do Until objFile2.AtEndOfStream
        strAddress = objFile2.ReadLine
        If InStr(strCurrentDevices, strAddress) = 0 Then
            strNotCurrent = strNotCurrent & strAddress & vbCrLf
        End If
    Loop
    objFile2.Close
    Wscript.Echo "Differences: " & vbCrLf & strNotCurrent
    Set objFile3 = objFSO.CreateTextFile("L:\Inbox\Test\Different.csv")
    objFile3.WriteLine strNotCurrent
    objFile3.Close

    • Edited by bstory00001 Friday, February 23, 2018 12:31 PM Added scripting
    • Moved by Bill_Stewart Monday, April 30, 2018 9:19 PM This is not "scripts on demand"
    Wednesday, February 21, 2018 3:31 PM

All replies