0
votes

VBScript expert I am not. I can use other peoples work to build what I want, but this one I'm having problems with....

Using VBScript, I need to compare the 2 most recent files in a folder, and if they are different, set an error code 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

I've found a ton of information, but nothing specifically doing what I'm looking for.

Thanks for any and all help!

Edit: Gotten closer to what I'm looking for....

'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 and set an errorlevel

***** This is the next part to figure out *****

1

1 Answers

0
votes

It's not pretty, and could be cleaned up, but maybe it will help someone else....

Option Explicit

Dim src, dst, f1, f2, mostRecent, objFSO, fso, objStartFolder, objFolder, colFiles, objFile, f, i, objFile1, strCurrentDevices, objFile2, objFile3, strAddress, strNotCurrent, cmd, strFile1, strFile2, arrFile1, arrFile2, intLineCount, strError

'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\*.csv"
   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

strFile1 = mostRecent(0)
strFile2 = mostRecent(1)
set objFSO = CreateObject("Scripting.FilesystemObject")
set objFile1 = objFSO.opentextfile(strFile1,1)
set objFile2 = objFSO.opentextfile(strFile2,1)
arrFile1 = split(objFile1.ReadAll,vbNewLine)
arrFile2 = split(objFile2.ReadAll,vbNewLine)
objFile1.close
objFile2.close

if ubound(arrFile1) < ubound(arrFile2) then
   intLineCount = ubound(arrFile1)
   strError = strFile2 & " is bigger than " & strFile1
elseif ubound(arrFile1) > ubound(arrFile2) then
   intLineCount = ubound(arrFile2)
   strError = strFile2 & " is bigger than " & strFile1
else 
   intLineCount = ubound(arrFile2)
end if

for i = 0 to intLineCount
   if not arrFile1(i) = arrFile2(i) then 
      exit for
   end if
next

if i < (intLineCount + 1) then
   WScript.Echo "Line " & (i+1) & " not equal"
'   WScript.Echo strError
'MISetErrorCode(1)
elseif strError <> "" then
'   WScript.Echo strError
else 
   WScript.Echo "Files are identical."
'MISetErrorCode(0)
end if