updateconn.vbs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. Function ReadIni( myFilePath, mySection, myKey )
  2. ' This function returns a value read from an INI file
  3. '
  4. ' Arguments:
  5. ' myFilePath [string] the (path and) file name of the INI file
  6. ' mySection [string] the section in the INI file to be searched
  7. ' myKey [string] the key whose value is to be returned
  8. '
  9. ' Returns:
  10. ' the [string] value for the specified key in the specified section
  11. '
  12. ' CAVEAT: Will return a space if key exists but value is blank
  13. '
  14. ' Written by Keith Lacelle
  15. ' Modified by Denis St-Pierre and Rob van der Woude
  16. Const ForReading = 1
  17. Const ForWriting = 2
  18. Const ForAppending = 8
  19. Dim intEqualPos
  20. Dim objFSO, objIniFile
  21. Dim strFilePath, strKey, strLeftString, strLine, strSection
  22. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  23. ReadIni = ""
  24. strFilePath = Trim( myFilePath )
  25. strSection = Trim( mySection )
  26. strKey = Trim( myKey )
  27. If objFSO.FileExists( strFilePath ) Then
  28. Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
  29. Do While objIniFile.AtEndOfStream = False
  30. strLine = Trim( objIniFile.ReadLine )
  31. ' Check if section is found in the current line
  32. If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
  33. strLine = Trim( objIniFile.ReadLine )
  34. ' Parse lines until the next section is reached
  35. Do While Left( strLine, 1 ) <> "["
  36. ' Find position of equal sign in the line
  37. intEqualPos = InStr( 1, strLine, "=", 1 )
  38. If intEqualPos > 0 Then
  39. strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
  40. ' Check if item is found in the current line
  41. If LCase( strLeftString ) = LCase( strKey ) Then
  42. ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
  43. ' In case the item exists but value is blank
  44. If ReadIni = "" Then
  45. ReadIni = " "
  46. End If
  47. ' Abort loop when item is found
  48. Exit Do
  49. End If
  50. End If
  51. ' Abort if the end of the INI file is reached
  52. If objIniFile.AtEndOfStream Then Exit Do
  53. ' Continue with next line
  54. strLine = Trim( objIniFile.ReadLine )
  55. Loop
  56. Exit Do
  57. End If
  58. Loop
  59. objIniFile.Close
  60. Else
  61. WScript.Echo strFilePath & " doesn't exists. Exiting..."
  62. Wscript.Quit 1
  63. End If
  64. End Function
  65. Sub WriteIni( myFilePath, mySection, myKey, myValue )
  66. ' This subroutine writes a value to an INI file
  67. '
  68. ' Arguments:
  69. ' myFilePath [string] the (path and) file name of the INI file
  70. ' mySection [string] the section in the INI file to be searched
  71. ' myKey [string] the key whose value is to be written
  72. ' myValue [string] the value to be written (myKey will be
  73. ' deleted if myValue is <DELETE_THIS_VALUE>)
  74. '
  75. ' Returns:
  76. ' N/A
  77. '
  78. ' CAVEAT: WriteIni function needs ReadIni function to run
  79. '
  80. ' Written by Keith Lacelle
  81. ' Modified by Denis St-Pierre, Johan Pol and Rob van der Woude
  82. Const ForReading = 1
  83. Const ForWriting = 2
  84. Const ForAppending = 8
  85. Dim blnInSection, blnKeyExists, blnSectionExists, blnWritten
  86. Dim intEqualPos
  87. Dim objFSO, objNewIni, objOrgIni, wshShell
  88. Dim strFilePath, strFolderPath, strKey, strLeftString
  89. Dim strLine, strSection, strTempDir, strTempFile, strValue
  90. strFilePath = Trim( myFilePath )
  91. strSection = Trim( mySection )
  92. strKey = Trim( myKey )
  93. strValue = Trim( myValue )
  94. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  95. Set wshShell = CreateObject( "WScript.Shell" )
  96. strTempDir = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
  97. strTempFile = objFSO.BuildPath( strTempDir, objFSO.GetTempName )
  98. Set objOrgIni = objFSO.OpenTextFile( strFilePath, ForReading, True )
  99. Set objNewIni = objFSO.CreateTextFile( strTempFile, False, False )
  100. blnInSection = False
  101. blnSectionExists = False
  102. ' Check if the specified key already exists
  103. blnKeyExists = ( ReadIni( strFilePath, strSection, strKey ) <> "" )
  104. blnWritten = False
  105. ' Check if path to INI file exists, quit if not
  106. strFolderPath = Mid( strFilePath, 1, InStrRev( strFilePath, "\" ) )
  107. If Not objFSO.FolderExists ( strFolderPath ) Then
  108. WScript.Echo "Error: WriteIni failed, folder path (" _
  109. & strFolderPath & ") to ini file " _
  110. & strFilePath & " not found!"
  111. Set objOrgIni = Nothing
  112. Set objNewIni = Nothing
  113. Set objFSO = Nothing
  114. WScript.Quit 1
  115. End If
  116. While objOrgIni.AtEndOfStream = False
  117. strLine = Trim( objOrgIni.ReadLine )
  118. If blnWritten = False Then
  119. If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
  120. blnSectionExists = True
  121. blnInSection = True
  122. ElseIf InStr( strLine, "[" ) = 1 Then
  123. blnInSection = False
  124. End If
  125. End If
  126. If blnInSection Then
  127. If blnKeyExists Then
  128. if strValue <> "" Then
  129. intEqualPos = InStr( 1, strLine, "=", vbTextCompare )
  130. If intEqualPos > 0 Then
  131. strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
  132. If LCase( strLeftString ) = LCase( strKey ) Then
  133. ' Only write the key if the value isn't empty
  134. ' Modification by Johan Pol
  135. If strValue <> "<DELETE_THIS_VALUE>" Then
  136. objNewIni.WriteLine strKey & "=" & strValue
  137. End If
  138. blnWritten = True
  139. blnInSection = False
  140. End If
  141. End If
  142. If Not blnWritten Then
  143. objNewIni.WriteLine strLine
  144. End If
  145. Else
  146. objNewIni.WriteLine strLine
  147. blnWritten = True
  148. blnInSection = False
  149. End If
  150. Else
  151. objNewIni.WriteLine strLine
  152. ' Only write the key if the value isn't empty
  153. ' Modification by Johan Pol
  154. If strValue <> "<DELETE_THIS_VALUE>" Then
  155. objNewIni.WriteLine strKey & "=" & strValue
  156. End If
  157. blnWritten = True
  158. blnInSection = False
  159. End If
  160. Else
  161. objNewIni.WriteLine strLine
  162. End If
  163. Wend
  164. If blnSectionExists = False Then ' section doesn't exist
  165. objNewIni.WriteLine
  166. objNewIni.WriteLine "[" & strSection & "]"
  167. ' Only write the key if the value isn't empty
  168. ' Modification by Johan Pol
  169. If strValue <> "<DELETE_THIS_VALUE>" Then
  170. objNewIni.WriteLine strKey & "=" & strValue
  171. End If
  172. End If
  173. objOrgIni.Close
  174. objNewIni.Close
  175. ' Delete old INI file
  176. objFSO.DeleteFile strFilePath, True
  177. ' Rename new INI file
  178. objFSO.MoveFile strTempFile, strFilePath
  179. Set objOrgIni = Nothing
  180. Set objNewIni = Nothing
  181. Set objFSO = Nothing
  182. Set wshShell = Nothing
  183. End Sub
  184. Dim path1,path2,sec,key
  185. path1 = "C:\RIDBMTReader\backup\tmp\conn.ini"
  186. path2 = "C:\RIDBMTReader\conn.ini"
  187. sec = "Config"
  188. key = "PORT"
  189. WriteIni path2, sec, key, ReadIni( path1, sec, key )
  190. key = "AutoOpenDir"
  191. WriteIni path2, sec, key, ReadIni( path1, sec, key )
  192. key = "FileName"
  193. WriteIni path2, sec, key, ReadIni( path1, sec, key )
  194. key = "ExportToFileByName"
  195. WriteIni path2, sec, key, ReadIni( path1, sec, key )
  196. key = "AllowSameFile"
  197. WriteIni path2, sec, key, ReadIni( path1, sec, key )