dcsimg
Results 1 to 3 of 3
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    How do I Update an INI file using a .vbs file

    How do I Update an INI file using a .vbs file? I have been playing around with this but to no avail. I have tried a number of diffent ways but I can't seem to get any that work. As you see I have a lin in my Network.ini file that is the 2nd line in the file "computername=Ghost" I need to have an input box to ask the new name and then insert that into the .ini file in the corrct place. Is this possible with .vbs files?

    const ForReading = 1, ForWriting = 2, ForAppending = 8
    dim fso,ts,strng
    dim compName
    compName= InputBox("Please enter the new name:")
    set fso=CreateObject("Scripting.FileSystemObject")
    set ts = fso.OpenTextFile("A:\Net\Network.ini", ForReading)
    Do
    if ts.Readline = "computername=Ghost" then
    WriteTheFile
    end if

    loop While Not ts.AtEndOfStream

    Function WriteTheFile
    set ts = fso.OpenTextFile("A:\Net\Network.ini", ForWriting, False)
    ts.writeline "computername=troy"

    End Function

    This is what the ini file looks like I need to change the computername with the variable from the inputbox:

    [network]
    computername=Ghost
    lanroot=A:\NET
    autostart=netbeui full
    username=tmacpherson
    domain=earth
    lslogon=yes
    reconnect=no
    passwordcaching=no
    timesync=no
    TMacPherson
    MIS Systems Engineer
    t_macpherson@yahoo.com


  2. #2
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    Reading / writing to INI files from VBScript or JavaScript is not very easy, though possible.
    Firstly, you will nead to read the entire contents into a structure in memory to search. This could be a dictionary object (see the article from 4guysfromrolla whic explains).
    Then you would need to update the key you want, then write the contents back to file with FSO.

    The Second approach, is to use VB code, encapsulated in a DLL to access the INI file with standard GetPrivateProfileString & WritePrivateProfileString API calls. This is then used for INI read, writes (There are loads of examples of this available - See here).

    I would take a different approach!
    Convert the INI file to XML (see how to do it here), this is easier to read/update than an INI file, and is capable of being delivered to the browser for client side access, where an INI file can't.

    That should keep you busy!
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: jerry@jg-design.net
    Working towards a bug free world......
    (Not a Microsoft employee)

  3. #3
    New Member
    Join Date
    Jan 2011
    Posts
    2

    Re: How do I Update an INI file using a .vbs file

    This is obviously very old but if anyone else is looking for this answer like I was.

    Here is a small script I drummed up.... The functions are not mine and all the credit goes to the original authors.



    strINIFile="MessingWithINI.ini"
    strSection="Test"
    strKey="Test"
    strValue="Test1"

    strTestKey = ReadIni(strINIFile, strSection, strKey)

    MsgBox strTestKey

    Call WriteIni(strINIFile, strSection, strKey, strValue)

    strTestKey = ReadIni(strINIFile, strSection, strKey)

    MsgBox strTestKey


    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

    Sub WriteIni( myFilePath, mySection, myKey, myValue )
    ' This subroutine writes a value to 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 written
    ' myValue [string] the value to be written (myKey will be
    ' deleted if myValue is <DELETE_THIS_VALUE>)
    '
    ' Returns:
    ' N/A
    '
    ' CAVEAT: WriteIni function needs ReadIni function to run
    '
    ' Written by Keith Lacelle
    ' Modified by Denis St-Pierre, Johan Pol and Rob van der Woude

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

    Dim blnInSection, blnKeyExists, blnSectionExists, blnWritten
    Dim intEqualPos
    Dim objFSO, objNewIni, objOrgIni, wshShell
    Dim strFilePath, strFolderPath, strKey, strLeftString
    Dim strLine, strSection, strTempDir, strTempFile, strValue

    strFilePath = Trim( myFilePath )
    strSection = Trim( mySection )
    strKey = Trim( myKey )
    strValue = Trim( myValue )

    Set objFSO = CreateObject( "Scripting.FileSystemObject" )
    Set wshShell = CreateObject( "WScript.Shell" )

    strTempDir = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
    strTempFile = objFSO.BuildPath( strTempDir, objFSO.GetTempName )

    Set objOrgIni = objFSO.OpenTextFile( strFilePath, ForReading, True )
    Set objNewIni = objFSO.CreateTextFile( strTempFile, False, False )

    blnInSection = False
    blnSectionExists = False
    ' Check if the specified key already exists
    blnKeyExists = ( ReadIni( strFilePath, strSection, strKey ) <> "" )
    blnWritten = False

    ' Check if path to INI file exists, quit if not
    'strFolderPath = Mid( strFilePath, 1, InStrRev( strFilePath, "\" ) )
    'If Not objFSO.FolderExists ( strFolderPath ) Then
    ' WScript.Echo "Error: WriteIni failed, folder path (" _
    ' & strFolderPath & ") to ini file " _
    ' & strFilePath & " not found!"
    ' Set objOrgIni = Nothing
    ' Set objNewIni = Nothing
    ' Set objFSO = Nothing
    ' WScript.Quit 1
    'End If

    While objOrgIni.AtEndOfStream = False
    strLine = Trim( objOrgIni.ReadLine )
    If blnWritten = False Then
    If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
    blnSectionExists = True
    blnInSection = True
    ElseIf InStr( strLine, "[" ) = 1 Then
    blnInSection = False
    End If
    End If

    If blnInSection Then
    If blnKeyExists Then
    intEqualPos = InStr( 1, strLine, "=", vbTextCompare )
    If intEqualPos > 0 Then
    strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
    If LCase( strLeftString ) = LCase( strKey ) Then
    ' Only write the key if the value isn't empty
    ' Modification by Johan Pol
    If strValue <> "<DELETE_THIS_VALUE>" Then
    objNewIni.WriteLine strKey & "=" & strValue
    End If
    blnWritten = True
    blnInSection = False
    End If
    End If
    If Not blnWritten Then
    objNewIni.WriteLine strLine
    End If
    Else
    objNewIni.WriteLine strLine
    ' Only write the key if the value isn't empty
    ' Modification by Johan Pol
    If strValue <> "<DELETE_THIS_VALUE>" Then
    objNewIni.WriteLine strKey & "=" & strValue
    End If
    blnWritten = True
    blnInSection = False
    End If
    Else
    objNewIni.WriteLine strLine
    End If
    Wend

    If blnSectionExists = False Then ' section doesn't exist
    objNewIni.WriteLine
    objNewIni.WriteLine "[" & strSection & "]"
    ' Only write the key if the value isn't empty
    ' Modification by Johan Pol
    If strValue <> "<DELETE_THIS_VALUE>" Then
    objNewIni.WriteLine strKey & "=" & strValue
    End If
    End If

    objOrgIni.Close
    objNewIni.Close

    ' Delete old INI file
    objFSO.DeleteFile strFilePath, True
    ' Rename new INI file
    objFSO.MoveFile strTempFile, strFilePath

    Set objOrgIni = Nothing
    Set objNewIni = Nothing
    Set objFSO = Nothing
    Set wshShell = Nothing
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width