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

  1. ' Windows Installer utility to add a transform or nested database as a substorage
  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 _Storages table
  5. '
  6. Option Explicit
  7.  
  8. Const msiOpenDatabaseModeReadOnly     = 0
  9. Const msiOpenDatabaseModeTransact     = 1
  10. Const msiOpenDatabaseModeCreate       = 3
  11.  
  12. Const msiViewModifyInsert         = 1
  13. Const msiViewModifyUpdate         = 2
  14. Const msiViewModifyAssign         = 3
  15. Const msiViewModifyReplace        = 4
  16. Const msiViewModifyDelete         = 6
  17.  
  18. Const ForAppending = 8
  19. Const ForReading = 1
  20. Const ForWriting = 2
  21. Const TristateTrue = -1
  22.  
  23. ' Check arg count, and display help if argument not present or contains ?
  24. Dim argCount:argCount = Wscript.Arguments.Count
  25. If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0
  26. If (argCount = 0) Then
  27.     Wscript.Echo "Windows Installer database substorage managment utility" &_
  28.         vbNewLine & " 1st argument is the path to MSI database (installer package)" &_
  29.         vbNewLine & " 2nd argument is the path to a transform or database to import" &_
  30.         vbNewLine & " If the 2nd argument is missing, substorages will be listed" &_
  31.         vbNewLine & " 3rd argument is optional, the name used for the substorage" &_
  32.         vbNewLine & " If the 3rd arugment is missing, the file name is used" &_
  33.         vbNewLine & " To remove a substorage, use /D or -D as the 2nd argument" &_
  34.         vbNewLine & " followed by the name of the substorage to remove" &_
  35.         vbNewLine &_
  36.         vbNewLine & "Copyright (C) Microsoft Corporation, 1999-2000.  All rights reserved."
  37.     Wscript.Quit 1
  38. End If
  39.  
  40. ' Connect to Windows Installer object
  41. On Error Resume Next
  42. Dim installer : Set installer = Nothing
  43. Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
  44.  
  45. ' Evaluate command-line arguments and set open and update modes
  46. Dim databasePath:databasePath = Wscript.Arguments(0)
  47. Dim openMode    : If argCount = 1 Then openMode = msiOpenDatabaseModeReadOnly Else openMode = msiOpenDatabaseModeTransact
  48. Dim updateMode  : If argCount > 1 Then updateMode = msiViewModifyAssign  'Either insert or replace existing row
  49. Dim importPath  : If argCount > 1 Then importPath = Wscript.Arguments(1)
  50. Dim storageName : If argCount > 2 Then storageName = Wscript.Arguments(2)
  51. If storageName = Empty And importPath <> Empty Then storageName = Right(importPath, Len(importPath) - InStrRev(importPath, "\",-1,vbTextCompare))
  52. If UCase(importPath) = "/D" Or UCase(importPath) = "-D" Then updateMode = msiViewModifyDelete : importPath = Empty 'substorage will be deleted if no input data
  53.  
  54. ' Open database and create a view on the _Storages table
  55. Dim sqlQuery : Select Case updateMode
  56.     Case msiOpenDatabaseModeReadOnly: sqlQuery = "SELECT `Name` FROM _Storages"
  57.     Case msiViewModifyAssign:         sqlQuery = "SELECT `Name`,`Data` FROM _Storages"
  58.     Case msiViewModifyDelete:         sqlQuery = "SELECT `Name` FROM _Storages WHERE `Name` = ?"
  59. End Select
  60. Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
  61. Dim view     : Set view = database.OpenView(sqlQuery)
  62. Dim record
  63.  
  64. If openMode = msiOpenDatabaseModeReadOnly Then 'If listing storages, simply fetch all records
  65.     Dim message, name
  66.     view.Execute : CheckError
  67.     Do
  68.         Set record = view.Fetch
  69.         If record Is Nothing Then Exit Do
  70.         name = record.StringData(1)
  71.         If message = Empty Then message = name Else message = message & vbNewLine & name
  72.     Loop
  73.     Wscript.Echo message
  74. Else 'If adding a storage, insert a row, else if removing a storage, delete the row
  75.     Set record = installer.CreateRecord(2)
  76.     record.StringData(1) = storageName
  77.     view.Execute record : CheckError
  78.     If importPath <> Empty Then  'Insert storage - copy data into stream
  79.         record.SetStream 2, importPath : CheckError
  80.     Else  'Delete storage, fetch first to provide better error message if missing
  81.         Set record = view.Fetch
  82.         If record Is Nothing Then Wscript.Echo "Storage not present:", storageName : Wscript.Quit 2
  83.     End If
  84.     view.Modify updateMode, record : CheckError
  85.     database.Commit : CheckError
  86.     Set view = Nothing
  87.     Set database = Nothing
  88.     CheckError
  89. End If
  90.  
  91. Sub CheckError
  92.     Dim message, errRec
  93.     If Err = 0 Then Exit Sub
  94.     message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  95.     If Not installer Is Nothing Then
  96.         Set errRec = installer.LastErrorRecord
  97.         If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText
  98.     End If
  99.     Wscript.Echo message
  100.     Wscript.Quit 2
  101. End Sub
  102.