Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readini.vbs

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

  1. ' Check number of command line arguments
  2. If WScript.Arguments.Count <> 3 Then Syntax
  3.  
  4. 'Parse the command line arguments
  5. ini     = WScript.Arguments(0)
  6. section = WScript.Arguments(1)
  7. key     = WScript.Arguments(2)
  8.  
  9. 'Initialize variables and constants
  10. Const ForReading = 1
  11. sectfound = False
  12. sect      = "[" & Strip( section ) & "]"
  13. keyfound  = False
  14.  
  15. 'Open INI file in read-only mode
  16. Set objFso = CreateObject( "Scripting.FileSystemObject" )
  17. Set handle = objFso.OpenTextFile( ini, ForReading )
  18.  
  19. 'Search the INI file for the specified key in the specified section
  20. Do While handle.AtEndOfStream <> True And keyfound = False
  21. 	line = Strip( handle.ReadLine )
  22. 	If Left( line, 1 ) = "[" Then
  23. 		If StrComp( line, sect, vbTextCompare ) = 0 Then
  24. 			sectfound = True
  25. 		Else
  26. 			sectfound = False
  27. 		End If
  28. 	Else
  29. 		If sectfound Then
  30. 			If InStr( 1, line, "=", vbTextCompare ) > 0 Then
  31. 				lineArray = Split( line, "=", 2, vbTextCompare )
  32. 				linekey = Strip( lineArray(0) )
  33. 				lineval = Strip( lineArray(1) )
  34. 				If StrComp( linekey, key, vbTextCompare ) = 0 Then
  35. 					keyval   = lineval
  36. 					keyfound = True
  37. 				End If
  38. 			End If
  39. 		End If
  40. 	End If
  41. Loop
  42.  
  43. 'Close the file and clear the object
  44. handle.close
  45. Set objFso = Nothing
  46.  
  47. 'Display the result
  48. strMsg = vbCrLf & Chr(34) & Strip( ini ) & Chr(34) & vbCrLf _
  49.        & sect & vbCrLf & key & "=" & keyval
  50. WScript.Echo strMsg
  51.  
  52.  
  53. 'Strip leading and trailing whitespace and quotes from a string
  54. Function Strip( mystring )
  55. 	Do While Left( mystring, 1 ) = " " Or Left( mystring, 1 ) = Chr(9)
  56. 		mystring = Mid( mystring, 2 )
  57. 	Loop
  58. 	Do While Right( mystring, 1 ) = " " Or Right( mystring, 1 ) = Chr(9)
  59. 		mystring = Mid( mystring, 1, Len( mystring ) - 1 )
  60. 	Loop
  61. 	If Left( mystring, 1 ) = Chr(34) Then
  62. 		mystring = Mid( mystring, 2 )
  63. 	End If
  64. 	If Right( mystring, 1 ) = Chr(34) Then
  65. 		mystring = Mid( mystring, 1, Len( mystring ) - 1 )
  66. 	End If
  67. 	Strip = mystring
  68. End Function
  69.  
  70.  
  71. Sub Syntax()
  72. 	strMsg = vbCrLf & "ReadINI.vbs,  Version 1.00" & vbCrLf _
  73. 	       & "Read a value from the specified INI file" & vbCrLf & vbCrLf _
  74. 	       & "Usage:  READINI.VBS  " & Chr(34) & "ini_file" & Chr(34) _
  75. 	       & "  " & Chr(34) & "section" & Chr(34) & "  " & Chr(34) _
  76. 	       & "key" & Chr(34) & vbCrLf & vbCrLf _
  77. 	       & "Where:               " & Chr(34) & "ini_file" & Chr(34) _
  78. 	       & " is the file name of the INI file to be read" & vbCrLf _
  79. 	       & "                     " & Chr(34) & "section" & Chr(34) _
  80. 	       & "  is the section name, without the brackets" & vbCrLf _
  81. 	       & "                     " & Chr(34) & "key" & Chr(34) _
  82. 	       & "      is the key whose value must be read" & vbCrLf & vbCrLf _
  83. 	       & "Example: if MYPROG.INI looks like this:" & vbCrLf _
  84. 	       & "[Section 1]" & vbCrLf _
  85. 	       & "Key1=Value 1" & vbCrLf _
  86. 	       & "Key2=Value 2" & vbCrLf _
  87. 	       & "[Section 2]" & vbCrLf _
  88. 	       & "Key1=Value 3" & vbCrLf _
  89. 	       & "Key2=Value 4" & vbCrLf & vbCrLf _
  90. 	       & "Then the command:  READINI.VBS  " & Chr(34) & "MYPROG.INI" _
  91. 	       & Chr(34) & "  " & Chr(34) & "section 2" & Chr(34) & "  " _
  92. 	       & Chr(34) & "key2" & Chr(34) & "" & vbCrLf _
  93. 	       & "should return:     Value 4" & vbCrLf & vbCrLf _
  94. 	       & "Written by Rob van der Woude" & vbCrLf _
  95. 	       & "http://www.robvanderwoude.com"
  96. 	WScript.Echo strMsg
  97. 	WScript.Quit 1
  98. End Sub
  99.  

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