Solved

VB script to modify text file

Posted on 2004-10-27
261 Views
Last Modified: 2008-01-16
We have an application with several settings stored in an ini file. as shown below

[Startup]
BinListPath="\\OldServer\ShareName\dir2\"
ViewCardData=True
ProgPath="\\OldServer\ShareName\dir2\"
UserDBPath="\\OldServer\ShareName\db\"
HistDBPath="\\OldServer\ShareName\dir2\"
CrdPath="\\OldServer\ShareName\dir3\"
ContrListPath="C:\Program Files\dir\IntelliCAT\"
ReportPath="\\OldServer\ShareName\Reports\"
CGSName="1401"
CGSPath="\\OldServer\ShareName\Queue\1401\"
CGSetup="True"
TimeOut="0"
LogoutEachTrns="TRUE"
IPTimeOut="255"
ManualFeed="False"
ShowGenie="True"
ReportPrinter="0"

[Communication]
ICCommPort=" 2"
IPCommPort=" 1"
Machine="data"

[Reports]
ReportName1="Export Database"
ReportFileName1="0\\OldServer\ShareName\Reports\Export_Database.rpt"

[DSN]
EKSNE="05C4AB64E01AFD18"

[Controller]
ServerName="1401"
Machine="machiner"
QueueLocation="\\OldServer\ShareName\Queue\1401"
WorkLocation="C:\Program Files\dir\program\"
ScanInt="3"

[Admin]
IPCommPort="1"
ICCommPort="1"
EquipType="equip"
EquipSettings="9600,e,7,1"
AdminCo="2"

[Window]
Config_Module_Settings="60,0,12000,8805"
Config_Module_State="0"
Config_Module_Last_Tab="0"

[REGISTRATION]
FName="Jane"
LName="doe"
CoName="company"
CoAddress="12345 anywhere"
CoCity="city"
CoState="state"
CoZip="zip"
CoPhone="12345678"
CoFax="12345678"

[ACTIVATION]
1234567

[RecentFiles]
File4="File5"
Path4="\\OldServer\ShareName\dir3\file1"
File3="file6"
Path3="\\OldServer\ShareName\dir3\file2"
File2="file7"
Path2="\\OldServer\ShareName\dir3\file3"
File1="file8"
Path1="\\OldServer\ShareName\dir3\file4"

we will be decomissioning OldServer soon and moving the database the application connects to NewSever.  So I need to create a script that will change all entries in the ini file from OldServer to NewServer all other lines must remain the same.  This file needs to be modified on approximatley 200 workstations.  Replacing the file is not an option as the software would require us to input a new activation number.
0
Question by:octhd
    3 Comments
     
    LVL 28

    Accepted Solution

    by:
    If you are looking to do this from VB,here is a function that may help. I used a command button to call the function as an example,but it could easily be turned into a command line utility that can be called from a logon script.



    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
    lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, _
    ByVal nSize As Long, ByVal lpFileName As String) As Long

    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
    lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long


    Private Sub Command1_Click()
    ReplaceServerInINI "C:\Test.ini", "\\OldServer\", "\\NewServer\"
    End Sub


    Private Sub ReplaceServerInINI(IniFileName As String, OldSvr As String, NewSvr As String)
    Dim ff As Integer, arrIni() As String, Ln As String, cnt As Integer
    Dim arrEnt() As String, Section As String, i As Integer
    cnt = 1

    ' open ini file and read into array
    ff = FreeFile
    Open IniFileName For Input As #ff
    Do Until EOF(ff)
    Line Input #ff, Ln

    ' get the section name
    If InStr(Ln, "[") > 0 And InStr(Ln, "]") > 0 Then
        Section = Replace$(Trim$(Ln), "[", "")
        Section = Replace$(Section, "]", "")
    End If

    ' if line has an "=" then get the values and add to array
    If InStr(Ln, "=") > 0 Then
        arrEnt = Split(Ln, "=")
        ReDim Preserve arrIni(cnt)
        arrIni(cnt) = Section & "<->" & arrEnt(0) & "<->" & arrEnt(1)
        cnt = cnt + 1
    End If
    Loop
    Close #ff

    Erase arrEnt()

    ' loop through array & use API to change entries in file
    For i = 1 To UBound(arrIni)
        arrEnt = Split(arrIni(i), "<->")
        Result = WritePrivateProfileString(arrEnt(0), arrEnt(1), Replace(arrEnt(2), OldSvr, NewSvr), IniFileName)
    Next i

    End Sub
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    If you are determined to use VBS files, however, you will have a problem with the above code, as VBS does not like Declare statements...

    In that case you will have to create a File System Object and do it line-by-line, the old-fashioned way...!!

    J.
    0
     
    LVL 76

    Assisted Solution

    by:David Lee
    Here's a sample script that uses the line-by-line approach that jimbobmcgee mentioned to make the changes.  All you need to do to use this is change the paths pointed to by strInputFile and strOutputFile.

        Dim objFSO, objTSInput, objTSOutput, strInputFile, strOutputFile, strBuffer
        strInputFile = "C:\EETesting\Testing.ini"
        strOutputFile = "C:\EETesting\NewFile.ini"
        Set objTSInput = objFSO.OpenTextFile(strInputFile)
        Set objTSOutput = objFSO.CreateTextFile(strOutputFile)
        While Not objTSInput.AtEndOfStream
            strBuffer = objTSInput.ReadLine
            strBuffer = Replace(strBuffer, "OldServer", "NewServer", , , vbTextCompare)
            objTSOutput.WriteLine strBuffer
        Wend
        objTSInput.Close
        objTSOutput.Close
        objFSO.CopyFile strOutputFile, strInputFile, True
        objFSO.DeleteFile strOutputFile
        Set objTSInput = Nothing
        Set objTSOutput = Nothing
        Set objFSO = Nothing
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Suggested Solutions

    Title # Comments Views Activity
    Binding controls to sql database table 2 37
    Port V2 16 30
    Use Multiple Forms 4 24
    VB6 code to programmatically convert pdf to excel 21 33
    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    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…

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now