vbScript, Place a line below another line in a text file

Lets Say I have a text file called "settings.ini" with the following contents

1451-Rapids-2324
2552-Birds-2324
2003-Dogs-2324
2324-Donkeys-2324
3455-Elephants-2324

Open in new window



I need a function like the following:
PlaceBelow("232", "5006-Tigers-3454")
 

Open in new window


When the above function is excecuted then it finds "232" starting only from the beginning of the the line and places the new "5006-Tigers-3454" string under that line in the file. So the result becomes:

1451-Rapids-2324
2552-Birds-2324
2003-Dogs-2324
2324-Donkeys-2324
5006-Tigers-3454
3455-Elephants-2324

Open in new window

LVL 1
New_AlexAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
Do you want to call a vbs passing the params or a call to a sub in the script that passes the params?
0
Bill PrewCommented:
Give this a try, it updates the INI file you specify adding the new line after the one you are looking for.

PlaceBelow "settings.ini", "232", "5006-Tigers-3454"

Function UpdateIni(sFile, sKey, sValue)
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oIniFile = oFSO.OpenTextFile(sFile, 1, False, -2)
    sLine = Split(oIniFile.ReadAll, VbCrLf)
    oIniFile.Close
    For i = 0 To UBound(sLine)-1
        If Left(sLine(i), Len(sKey)) = sKey Then
          sLine(i) = sLine(i) & vbCrLf & sValue
        End If
    Next
    Set oIniFile = oFSO.OpenTextFile(sFile, 2, True)
    oIniFile.Write(Join(sLine, vbCrLf))
    oIniFile.Close
    Set oIniFile = Nothing
    Set oFSO = Nothing
End Function

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris BottomleySoftware Quality Lead EngineerCommented:
Either call it from the command line as supplied using:

savednameas.vbs "232" "1234-1bcd-f"

or replace:

set param=wscript.Arguments
call PlaceBelow  (param(0), param(1))

in the script with:

Call PlaceBelow  ("232" "1234-1bcd-f")

Chris
set param=wscript.Arguments 
call PlaceBelow  (param(0), param(1)) 

Sub PlaceBelow(param1, param2)

    strfile = "c:\deleteme\settings.ini"
    Set FSO = CreateObject("scripting.filesystemobject")
    Set fil = FSO.opentextfile(strfile, 1, True)
    If fil.atendofstream Then
        arr = Array(0)
        arr(0) = ""
        ReDim nuarr(0)
        lngLineCount = 0
    Else
        arr = Split(fil.readall, vbCrLf)
        ReDim nuarr(UBound(arr) + 1)
        lngLineCount = UBound(arr)
    End If
    fil.Close
    For lngLine = 0 To lngLineCount
        lin = arr(lngLine - lngLineIncrement)
        elem = Split(lin, "-")
        If lngLineIncrement = 1 Then
            nuarr(lngLine + lngLineIncrement) = arr(lngLine)
        ElseIf lin = "" Then
            nuarr(lngLine + lngLineIncrement) = param2
            lngLineIncrement = 1
        ElseIf Left(elem(0) & "0000", 4) <= Left(param1 & "0000", 4) Then
            nuarr(lngLine + lngLineIncrement) = arr(lngLine)
        Else
            nuarr(lngLine + lngLineIncrement) = param2
            lngLineIncrement = 1
            nuarr(lngLine + lngLineIncrement) = arr(lngLine)
        End If
    Next
    Set fil = FSO.createtextfile(strfile, True)
    fil.Write Join(nuarr, vbCrLf)
    fil.Close
End Sub

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Chris BottomleySoftware Quality Lead EngineerCommented:
Missed a comma in the edits to call the script from within therefore reproduced here with the correction:

As in the above post to call as a vbscript from the command
As in the snippet below to call as a function from within a vb script.

Chris
call placebelow ("232", "1234-1bcd-f")

Sub placebelow(param1, param2)

    strfile = "c:\deleteme\settings.ini"
    Set FSO = CreateObject("scripting.filesystemobject")
    Set fil = FSO.opentextfile(strfile, 1, True)
    If fil.atendofstream Then
        arr = Array(0)
        arr(0) = ""
        ReDim nuarr(0)
        lngLineCount = 0
    Else
        arr = Split(fil.readall, vbCrLf)
        ReDim nuarr(UBound(arr) + 1)
        lngLineCount = UBound(arr)
    End If
    fil.Close
    For lngLine = 0 To lngLineCount
        lin = arr(lngLine - lngLineIncrement)
        elem = Split(lin, "-")
        If lngLineIncrement = 1 Then
            nuarr(lngLine + lngLineIncrement) = arr(lngLine)
        ElseIf lin = "" Then
            nuarr(lngLine + lngLineIncrement) = param2
            lngLineIncrement = 1
        ElseIf Left(elem(0) & "0000", 4) <= Left(param1 & "0000", 4) Then
            nuarr(lngLine + lngLineIncrement) = arr(lngLine)
        Else
            nuarr(lngLine + lngLineIncrement) = param2
            lngLineIncrement = 1
            nuarr(lngLine + lngLineIncrement) = arr(lngLine)
        End If
    Next
    Set fil = FSO.createtextfile(strfile, True)
    fil.Write Join(nuarr, vbCrLf)
    fil.Close
End Sub

Open in new window

0
New_AlexAuthor Commented:
Hi guys.

Thanks billthrou  this is what I wanted exactly. The code is small and cool

Thanks also CHris even if your code has some extra functionality that was not asked, it is a very good attempt

Thanks guys
0
Bill PrewCommented:
Glad that helped, thanks.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.