' Check number of command line arguments If WScript.Arguments.Count <> 3 Then Syntax 'Parse the command line arguments ini = WScript.Arguments(0) section = WScript.Arguments(1) key = WScript.Arguments(2) 'Initialize variables and constants Const ForReading = 1 sectfound = False sect = "[" & Strip( section ) & "]" keyfound = False 'Open INI file in read-only mode Set objFso = CreateObject( "Scripting.FileSystemObject" ) Set handle = objFso.OpenTextFile( ini, ForReading ) 'Search the INI file for the specified key in the specified section Do While handle.AtEndOfStream <> True And keyfound = False line = Strip( handle.ReadLine ) If Left( line, 1 ) = "[" Then If StrComp( line, sect, vbTextCompare ) = 0 Then sectfound = True Else sectfound = False End If Else If sectfound Then If InStr( 1, line, "=", vbTextCompare ) > 0 Then lineArray = Split( line, "=", 2, vbTextCompare ) linekey = Strip( lineArray(0) ) lineval = Strip( lineArray(1) ) If StrComp( linekey, key, vbTextCompare ) = 0 Then keyval = lineval keyfound = True End If End If End If End If Loop 'Close the file and clear the object handle.close Set objFso = Nothing 'Display the result strMsg = vbCrLf & Chr(34) & Strip( ini ) & Chr(34) & vbCrLf _ & sect & vbCrLf & key & "=" & keyval WScript.Echo strMsg 'Strip leading and trailing whitespace and quotes from a string Function Strip( mystring ) Do While Left( mystring, 1 ) = " " Or Left( mystring, 1 ) = Chr(9) mystring = Mid( mystring, 2 ) Loop Do While Right( mystring, 1 ) = " " Or Right( mystring, 1 ) = Chr(9) mystring = Mid( mystring, 1, Len( mystring ) - 1 ) Loop If Left( mystring, 1 ) = Chr(34) Then mystring = Mid( mystring, 2 ) End If If Right( mystring, 1 ) = Chr(34) Then mystring = Mid( mystring, 1, Len( mystring ) - 1 ) End If Strip = mystring End Function Sub Syntax() strMsg = vbCrLf & "ReadINI.vbs, Version 1.00" & vbCrLf _ & "Read a value from the specified INI file" & vbCrLf & vbCrLf _ & "Usage: READINI.VBS " & Chr(34) & "ini_file" & Chr(34) _ & " " & Chr(34) & "section" & Chr(34) & " " & Chr(34) _ & "key" & Chr(34) & vbCrLf & vbCrLf _ & "Where: " & Chr(34) & "ini_file" & Chr(34) _ & " is the file name of the INI file to be read" & vbCrLf _ & " " & Chr(34) & "section" & Chr(34) _ & " is the section name, without the brackets" & vbCrLf _ & " " & Chr(34) & "key" & Chr(34) _ & " is the key whose value must be read" & vbCrLf & vbCrLf _ & "Example: if MYPROG.INI looks like this:" & vbCrLf _ & "[Section 1]" & vbCrLf _ & "Key1=Value 1" & vbCrLf _ & "Key2=Value 2" & vbCrLf _ & "[Section 2]" & vbCrLf _ & "Key1=Value 3" & vbCrLf _ & "Key2=Value 4" & vbCrLf & vbCrLf _ & "Then the command: READINI.VBS " & Chr(34) & "MYPROG.INI" _ & Chr(34) & " " & Chr(34) & "section 2" & Chr(34) & " " _ & Chr(34) & "key2" & Chr(34) & "" & vbCrLf _ & "should return: Value 4" & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" & vbCrLf _ & "http://www.robvanderwoude.com" WScript.Echo strMsg WScript.Quit 1 End Sub