[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-13
6
Medium Priority
?
249 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:New_Alex
  • 3
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36961660
Do you want to call a vbs passing the params or a call to a sub in the script that passes the params?
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1200 total points
ID: 36961699
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36961768
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 800 total points
ID: 36961838
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
 
LVL 1

Author Closing Comment

by:New_Alex
ID: 36967817
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36967848
Glad that helped, thanks.

~bp
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 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