'Script to update a user scenario after applying a patch
'It requires the original scenario the moded one is based on, the updated original scenario and of course the moded scenario 
'It will creates new names for the updated scenarios. Any modified entries and conflicts are written into a logfile
'By A. Ramseier, 21-Sep-09

Const ForReading = 1, ForWriting = 2
Dim nScenOrig,nScenMod,sDirOrig,sDirUpd,sDirMod
Dim aType(7)
'Change directory here
sDirOrig="C:\compcsv\WITP"
sDirUpd="C:\compcsv\WITP"
sDirMod="C:\compcsv\WITP"

'Change name of files here
nScenOrig="001"
nScenMod="027"
nScenUpd="001Upd"


'-----------------------------------
aType(0)="grp"
aType(1)="shp"
aType(2)="plt"
aType(3)="loc"
aType(4)="ldr"
aType(5)="dev"
aType(6)="cls"
aType(7)="air"

For each sType in aType
	CompFiles nScenOrig,nScenMod,nScenUpd,sType,sDirOrig,sDirMod,sDirUpd
Next

Wscript.Echo "Done"

Sub CompFiles(nScenOrig, nScenMod,nScenUpd, sType,sDirOrig,sDirMod,sDirUpd)
Dim fso, csvOrig,csvMod, strLine1, strLine2, strLine3, a1, a2, a3, nLC
Set fso = CreateObject("Scripting.FileSystemObject")
Set csvOrig = fso.OpenTextFile(sDirOrig & sType & nScenOrig & ".csv", ForReading)
Set csvMod = fso.OpenTextFile(sDirMod & sType & nScenMod & ".csv", ForReading)
Set csvUpd = fso.OpenTextFile(sDirUpd & sType & nScenUpd & ".csv", ForReading)
Set csvResult = fso.CreateTextFile(sDirUpd & sType & nScenMod & "Upd.csv")
Set csvLog = fso.OpenTextFile(sDirUpd & nScenMod & "Mod.log",8,true)

Do Until csvUpd.AtEndOfStream
    strLine1 = csvUpd.Readline
		strLine2 = csvOrig.Readline
		strLine3 = csvMod.Readline
      If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
          csvResult.writeline strLine3
      Else
      	a1=Split(strLine1,",")
      	a2=Split(strLine2,",")
      	a3=Split(strLine3,",")

      	For nLC=1 To UBound(a1)
      		If a3(nLC)<>a1(nLC) And a3(nLC)=a2(nLC) Then	
      			csvLog.writeline "Modified Entry in File: WITP" & nScenMod & sType & "Upd.csv on Line: " & csvUpd.Line-1  & " Column: " & nLC+1 &  " Original: " & a2(nLC) & " Update: " &  a1(nLC) & " Mod: " & a3(nLC)
      			a3(nLC)=a1(nLC)
      		ElseIf a3(nLC)<>a1(nLC) And a3(nLC)<>a2(nLC) And a1(nLC)<>a2(nLC) Then
      			csvLog.writeline "Conflict in File: WITP" & nScenMod & sType & "Upd.csv on Line: " & csvUpd.Line-1  & " Column: " & nLC+1 &  " Original: " & a2(nLC) & " Update: " &  a1(nLC) & " Mod: " & a3(nLC)
      		End If
      	Next
      	csvResult.writeline join(a3,",")     	    		 
      End If 
Loop

csvResult.Close
csvOrig.Close
csvMod.Close
csvLog.Close
csvUpd.Close
End Sub






