Solved

Vbscript needed for reading value from a file.

Posted on 2008-06-17
1
484 Views
Last Modified: 2012-05-05
I need to read the value of a parameter from a file.

Which code do I have to use if I the value of the parameter is present in the file in this format.

parameter1=value1
parameter2=value2

How can I retrieve eg. the value of parameter2 ?
0
Comment
Question by:itbabe
1 Comment
 
LVL 7

Accepted Solution

by:
Zippit earned 500 total points
ID: 21802120
Here is a function that will do so for you.
MsgBox getValue("C:\Documents and Settings\username\Desktop\sample.txt", "parameter1")
 
 
 
 
Function getValue(filePath, paramName)
 
    Dim fso     'As Scripting.FileSystemObject
    Dim ts      'As Scripting.TextStream
    Dim line    'As String
    Dim tokens  'As Variant
    Dim rv      'As String
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(filePath, ForReading)
    
    Do While Not ts.AtEndOfStream
        line = ts.ReadLine
    
        tokens = Split(line, "=")
        
        If Trim(LCase(tokens(0))) = Trim(LCase(paramName)) Then
            rv = tokens(1)
            Exit Do
        End If
    Loop
 
    Set fso = Nothing
 
    getValue = rv
End Function

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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