Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.4 KiB

  1. ' Windows Installer database table import for use with Windows Scripting Host
  2. ' Copyright (c) 1999, Microsoft Corporation
  3. ' Demonstrates the use of the Database.Import method and MsiDatabaseImport API
  4. '
  5. Option Explicit
  6. Const msiOpenDatabaseModeReadOnly = 0
  7. Const msiOpenDatabaseModeTransact = 1
  8. Const msiOpenDatabaseModeCreate = 3
  9. Const ForAppending = 8
  10. Const ForReading = 1
  11. Const ForWriting = 2
  12. Const TristateTrue = -1
  13. Dim argCount:argCount = Wscript.Arguments.Count
  14. Dim iArg:iArg = 0
  15. If (argCount < 3) Then
  16. Wscript.Echo "Windows Installer database table import utility" &_
  17. vbNewLine & " 1st argument is the path to MSI database (installer package)" &_
  18. vbNewLine & " 2nd argument is the path to folder containing the imported files" &_
  19. vbNewLine & " Subseqent arguments are names of archive files to import" &_
  20. vbNewLine & " Wildcards, such as *.idt, can be used to import multiple files" &_
  21. vbNewLine & " Specify /c or -c anywhere before file list to create new database"
  22. Wscript.Quit 1
  23. End If
  24. ' Connect to Windows Installer object
  25. On Error Resume Next
  26. Dim installer : Set installer = Nothing
  27. Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
  28. Dim openMode:openMode = msiOpenDatabaseModeTransact
  29. Dim databasePath:databasePath = NextArgument
  30. Dim folder:folder = NextArgument
  31. ' Open database and process list of files
  32. Dim database, table
  33. Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
  34. While iArg < argCount
  35. table = NextArgument
  36. ' Check file name for wildcard specification
  37. If (InStr(1,table,"*",vbTextCompare) <> 0) Or (InStr(1,table,"?",vbTextCompare) <> 0) Then
  38. ' Obtain list of files matching wildcard specification
  39. Dim WshShell, fileSys, file, tempFilePath
  40. Set WshShell = Wscript.CreateObject("Wscript.Shell") : CheckError
  41. tempFilePath = WshShell.ExpandEnvironmentStrings("%TEMP%") & "\dir.tmp"
  42. WshShell.Run "cmd.exe /U /c dir /b " & folder & "\" & table & ">" & tempFilePath, 0, True : CheckError
  43. Set fileSys = CreateObject("Scripting.FileSystemObject") : CheckError
  44. Set file = fileSys.OpenTextFile(tempFilePath, ForReading, False, TristateTrue) : CheckError
  45. ' Import each file in directory list
  46. Do While file.AtEndOfStream <> True
  47. table = file.ReadLine
  48. database.Import folder, table : CheckError
  49. Loop
  50. Else
  51. database.Import folder, table : CheckError
  52. End If
  53. Wend
  54. database.Commit 'commit changes if no import errors
  55. Wscript.Quit 0
  56. Function NextArgument
  57. Dim arg, chFlag
  58. Do
  59. arg = Wscript.Arguments(iArg)
  60. iArg = iArg + 1
  61. chFlag = AscW(arg)
  62. If (chFlag = AscW("/")) Or (chFlag = AscW("-")) Then
  63. chFlag = UCase(Right(arg, Len(arg)-1))
  64. If chFlag = "C" Then
  65. openMode = msiOpenDatabaseModeCreate
  66. Else
  67. Wscript.Echo "Invalid option flag:", arg : Wscript.Quit 1
  68. End If
  69. Else
  70. Exit Do
  71. End If
  72. Loop
  73. NextArgument = arg
  74. End Function
  75. Sub CheckError
  76. Dim message, errRec
  77. If Err = 0 Then Exit Sub
  78. message = "ERROR: " & Err.Source & " " & Hex(Err) & ": " & Err.Description
  79. If Not installer Is Nothing Then
  80. Set errRec = installer.LastErrorRecord
  81. If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText
  82. End If
  83. Wscript.Echo message
  84. Wscript.Quit 2
  85. End Sub