home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / Chip_2001-06_cd1.bin / zkuste / vbasic / Data / Utility / MSISDK15.msi / WiMerge.vbs < prev    next >
Text File  |  2000-10-05  |  3KB  |  72 lines

  1. ' Windows Installer database utility to merge data from another database              
  2. ' For use with Windows Scripting Host, CScript.exe or WScript.exe
  3. ' Copyright (c) 1999-2000, Microsoft Corporation
  4. ' Demonstrates the use of the Database.Merge method and MsiDatabaseMerge API
  5. '
  6. Option Explicit
  7.  
  8. Const msiOpenDatabaseModeReadOnly     = 0
  9. Const msiOpenDatabaseModeTransact     = 1
  10. Const msiOpenDatabaseModeCreate       = 3
  11. Const ForAppending = 8
  12. Const ForReading = 1
  13. Const ForWriting = 2
  14. Const TristateTrue = -1
  15.  
  16. Dim argCount:argCount = Wscript.Arguments.Count
  17. Dim iArg:iArg = 0
  18. If (argCount < 2) Then
  19.     Wscript.Echo "Windows Installer database merge utility" &_
  20.         vbNewLine & " 1st argument is the path to MSI database (installer package)" &_
  21.         vbNewLine & " 2nd argument is the path to database containing data to merge" &_
  22.         vbNewLine & " 3rd argument is the optional table to contain the merge errors" &_
  23.         vbNewLine & " If 3rd argument is not present, the table _MergeErrors is used" &_
  24.         vbNewLine & "  and that table will be dropped after displaying its contents." &_
  25.         vbNewLine &_
  26.         vbNewLine & "Copyright (C) Microsoft Corporation, 1999-2000.  All rights reserved."
  27.     Wscript.Quit 1
  28. End If
  29.  
  30. ' Connect to Windows Installer object
  31. On Error Resume Next
  32. Dim installer : Set installer = Nothing
  33. Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
  34.  
  35. ' Open databases and merge data
  36. Dim database1 : Set database1 = installer.OpenDatabase(WScript.Arguments(0), msiOpenDatabaseModeTransact) : CheckError
  37. Dim database2 : Set database2 = installer.OpenDatabase(WScript.Arguments(1), msiOpenDatabaseModeReadOnly) : CheckError
  38. Dim errorTable : errorTable = "_MergeErrors"
  39. If argCount >= 3 Then errorTable = WScript.Arguments(2)
  40. Dim hasConflicts:hasConflicts = database1.Merge(database2, errorTable) 'Old code returns void value, new returns boolean
  41. If hasConflicts <> True Then hasConflicts = CheckError 'Temp for old Merge function that returns void
  42. If hasConflicts <> 0 Then
  43.     Dim message, line, view, record
  44.     Set view = database1.OpenView("Select * FROM `" & errorTable & "`") : CheckError
  45.     view.Execute
  46.     Do
  47.         Set record = view.Fetch
  48.         If record Is Nothing Then Exit Do
  49.         line = record.StringData(1) & " table has " & record.IntegerData(2) & " conflicts"
  50.         If message = Empty Then message = line Else message = message & vbNewLine & line
  51.     Loop
  52.     Set view = Nothing
  53.     Wscript.Echo message
  54. End If
  55. If argCount < 3 And hasConflicts Then database1.OpenView("DROP TABLE `" & errorTable & "`").Execute : CheckError
  56. database1.Commit : CheckError
  57. Quit 0
  58.  
  59. Function CheckError
  60.     Dim message, errRec
  61.     CheckError = 0
  62.     If Err = 0 Then Exit Function
  63.     message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  64.     If Not installer Is Nothing Then
  65.         Set errRec = installer.LastErrorRecord
  66.         If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText : CheckError = errRec.IntegerData(1)
  67.     End If
  68.     If CheckError = 2268 Then Err.Clear : Exit Function
  69.     Wscript.Echo message
  70.     Wscript.Quit 2
  71. End Function
  72.