Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Script to Read section of a file

Posted on 2014-03-16
5
Medium Priority
?
591 Views
Last Modified: 2014-03-18
Hey guys,

I am using BGInfo.exe by SysInternals to display some information on my stations. I have a file located in the C:\Windows directory called station.ini. It contains the following:

[SETTINGS]
Program=C:\Path\App.exe
PCNum=25


I would like to write a VB script that would return the PCNum, in this case "25" or returned "Not Installed".

I have never written a VBS in my life and also not very familiar with BGInfo. Any help is appreciated. Thanks!
0
Comment
Question by:triphen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

by:triphen
ID: 39933299
OK after some research I found this:

Function ReadIni( myFilePath, mySection, myKey )
    ' This function returns a value read from an INI file
    '
    ' Arguments:
    ' myFilePath  [string]  the (path and) file name of the INI file
    ' mySection   [string]  the section in the INI file to be searched
    ' myKey       [string]  the key whose value is to be returned
    '
    ' Returns:
    ' the [string] value for the specified key in the specified section
    '
    ' CAVEAT:     Will return a space if key exists but value is blank
    '
    ' Written by Keith Lacelle
    ' Modified by Denis St-Pierre and Rob van der Woude

    Const ForReading   = 1
    Const ForWriting   = 2
    Const ForAppending = 8

    Dim intEqualPos
    Dim objFSO, objIniFile
    Dim strFilePath, strKey, strLeftString, strLine, strSection

    Set objFSO = CreateObject( "Scripting.FileSystemObject" )

    ReadIni     = ""
    strFilePath = Trim( myFilePath )
    strSection  = Trim( mySection )
    strKey      = Trim( myKey )

    If objFSO.FileExists( strFilePath ) Then
        Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
        Do While objIniFile.AtEndOfStream = False
            strLine = Trim( objIniFile.ReadLine )

            ' Check if section is found in the current line
            If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
                strLine = Trim( objIniFile.ReadLine )

                ' Parse lines until the next section is reached
                Do While Left( strLine, 1 ) <> "["
                    ' Find position of equal sign in the line
                    intEqualPos = InStr( 1, strLine, "=", 1 )
                    If intEqualPos > 0 Then
                        strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
                        ' Check if item is found in the current line
                        If LCase( strLeftString ) = LCase( strKey ) Then
                            ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
                            ' In case the item exists but value is blank
                            If ReadIni = "" Then
                                ReadIni = " "
                            End If
                            ' Abort loop when item is found
                            Exit Do
                        End If
                    End If

                    ' Abort if the end of the INI file is reached
                    If objIniFile.AtEndOfStream Then Exit Do

                    ' Continue with next line
                    strLine = Trim( objIniFile.ReadLine )
                Loop
            Exit Do
            End If
        Loop
        objIniFile.Close
    Else
        WScript.Echo strFilePath & " doesn't exists. Exiting..."
        Wscript.Quit 1
    End If
End Function


WScript.Echo ReadIni("C:\WINDOWS\station.INI", "SETTINGS", "PCNum")

Open in new window



When running the .VBS file on my desktop, a message box pops up and says 25 as expected. However, when I load this VBS file into BGInfo it returns this error...


---------------------------
BGInfo
---------------------------
Error evaluating scripted field 'Test'
Microsoft VBScript runtime error
Line 76, position 0
Object required: 'WScript'
---------------------------
OK  
---------------------------
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 2000 total points
ID: 39934490
After some googling... this seems to be the preferred way to go...

On Error Resume Next
      call WScript.Echo(strReturn)      'for cmd line
      call Echo(strReturn)      'for BGInfo
on error goto 0


When used insdie BGinfo the "Wscript" call will not work correctly.   When used on command line, the WScript is required... so the On error surrounding the two will allow both to be executed, and you should see the results of whichever one works depending upon how it is being run at the time.

So in your case... I'd try replacing :

WScript.Echo ReadIni("C:\WINDOWS\station.INI", "SETTINGS", "PCNum")

with this:

On Error Resume Next
      WScript.Echo ReadIni("C:\WINDOWS\station.INI", "SETTINGS", "PCNum")
      Echo ReadIni("C:\WINDOWS\station.INI", "SETTINGS", "PCNum")
on error goto 0
0
 

Author Closing Comment

by:triphen
ID: 39934519
SWEEET! Thank you sir!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39935058
BTW You can do a line or two of batch file (and quite a lot less VBS) if you want to, e.g.

@echo off
Set PCName=Not Installed
for /f "tokens=1*, delims==" %%a in (c:\windows\station.ini) do if "%%a"="PCName" SET PCName=%%~b
0
 

Author Comment

by:triphen
ID: 39938429
Thanks for that dragon-it!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question