Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for inifuncs.vbs

(view source code of inifuncs.vbs as plain text)

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

page last modified: 2024-04-16; loaded in 0.0083 seconds