[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Add 1 Line to a File at a Particular Point??

Posted on 2004-04-08
Medium Priority
Last Modified: 2010-05-02
I need to add one line, at a particular point (after a particular line) in a file, this file is called machine.config. This needs to be done on a number of machines in the network.

Basically the psuedo code would look something like:

'do for all the machines on the network BHW0 to BHW86
for n=0; n < 86; n++ {

    find file machine.config on machine BHn

    line_number = 0

    'Search through lines of file until particular line found


    until ((line_number = num_of_lines_in_file) OR (content_at_line_number = "trust = medium"))

    'If line is found then add the new line and save
    if line_number < num_of_lines_in_file THEN
        add a line to the file
        save the file
    end if  



Very sorry in advance for the bad coding, I am not a programmer, I work in network support.

Is the above possible?

What language is best to use?

Any help would be much appreciated.


Question by:options_technology
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
LVL 17

Expert Comment

ID: 10781863
Is it like an INI file, is there a keyword that you can search for?
LVL 17

Expert Comment

ID: 10781887
It could be like this (I will post the source code for the functions later, if there is a need).

sFileName = "......\machine.config"

' read data from file
sData = GF.ReadFile(sFileName)

' get the bit to the left of the keyword
sLeft = GF.LeftPart(sData, vbcrlf + "Trust =")

' create line to be inserted
sInsert = "NewField = Something"

' stich it all back together
sData = sLeft + vbcrlf + sInsert + vbcrlf + Mid(sData, len(sLleft)+2)

' save data
OK = GF.WriteFile(sFileName, sData)
LVL 17

Accepted Solution

zzzzzooc earned 300 total points
ID: 10781892
Something like this *should* work..


Private Sub Form_Load()
    If AddLine("c:\test.txt", "trust = medium", "my new line") = True Then
        MsgBox "Line added."
    End If
End Sub
Private Function AddLine(ByVal sFile As String, sAfterLine As String, sNewLine As String) As Boolean
    Dim sBuff As String, sLines() As String, iFF As Integer
    Dim iLoop1 As Integer, iLoop2 As Integer
    Dim bFound As Boolean, bDone As Boolean
    'If file exists..
    If Dir(sFile, vbNormal + vbHidden) <> vbNullString Then
        iFF = FreeFile
        Open sFile For Binary As iFF
            sBuff = Space(LOF(iFF))
            Get #iFF, 1, sBuff
        Close iFF
        iFF = FreeFile
        Open sFile For Output As iFF
            sLines = Split(sBuff, vbCrLf)
            For iLoop1 = 0 To UBound(sLines)
                If sAfterLine = sLines(iLoop1) Then
                    bFound = True
                    For iLoop2 = 0 To UBound(sLines)
                        If iLoop1 >= iLoop2 Or bDone = True Then
                            Print #iFF, sLines(iLoop2)
                            Print #iFF, sNewLine
                            iLoop2 = iLoop2 - 1
                            bDone = True
                        End If
                    Next iLoop2
                End If
            Next iLoop1
            If bFound = False Then Print #iFF, sNewLine
            AddLine = True
        Close iFF
    End If
End Function
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 17

Expert Comment

ID: 10781916
Or the old way:

Open sFileName For Input As #1
Open "temp.tmp" For Output as #2

Dim lc as long
Dim L$
Do While Not Eof(1)
    lc= lc + 1 ' line count
    Line Input #1, L$
    If Left(L$, 5)="Trust" Then ' or say If lc = 123 Then
        Print #2, "NewField = Something" ' insrt a new line
        bReplace = False ' is this replace or insert? set = True if replace
    End If
    If Not bReplace Then
        Print #2, L$ ' save the current line
    End If
Close 1
Close 2

Author Comment

ID: 10781921
Windows says the file type is a:

Web Configuration file

The file extension is .config

The keyword that could be searched for to determine which place to add the line after is:




LVL 17

Expert Comment

ID: 10781928
Change above as follows, in order to allow insert a line or change a line:

Dim L$
dim bReplace As Boolean ' forgot this
Do While Not Eof(1)
    bReplace = False ' and this
    lc= lc + 1 ' line count
LVL 17

Expert Comment

ID: 10781948
As at the end:

Close 1
Close 2
If Len(Dir("Machine.bak")) Then
    Kill "machine.bak"
End If
' don;t do these lines until sure its working ok

Name sFileName As "machine.bak" ' create a backup
Name "temp.tmp" as sFileName ' replace the original file

Author Comment

ID: 10782073
Thanks loads for the help so far.

Does anyone know the way to do this on a number of machines on a network?


for n=0; n < 86; n++ {

    find file machine.config on machine BHWn

    change file on BHWn


This is the bit that would really speed up a number of changes on our network.



Author Comment

ID: 10782173
Also the location of the file changes from PC to PC.

Would it be possible to use a VB file search API, for example from:


and then when searching for the file use something like:

for n=0; n < 86; n++ {

    searchdrive =  "\\bhw" + n + "\C$"

    filelocation = searchfunction("machine.config",searchdrive)

    if filelocation <> null then
        run file changing code
    end if

This is only a guess, my apologies if it is a totally stupid idea.


LVL 17

Expert Comment

ID: 10782220
So you basically want to enumerate through all computers on the network and then enumerate through all their files remotely and then modify one if it's found..? Should have chosen a different topic title since that'd be the difficult part.

If the files aren't on a similar mapped drive nor in a similar shared folder.. I'm not sure how to possibly go about all of that using windows' file operations.

Going to sleep anyways.. :)~
LVL 17

Assisted Solution

inthedark earned 300 total points
ID: 10789944
The simplest way to do this is to call an exe form your system login script.

The exe should decide if a  change has been made since the last run as it may not need to run.

I use a class for file operations, here is an example:

Private Sub Form_Load()

Dim FOP As New zFOP ' my file operations class
Dim Drives As Collection
Dim Drive
Dim sPath As String

Set Drives = FOP.GetDrives ' get a list of local hard drives

' loop through each drive
For Each Drive In Drives
    sPath = FOP.LocateFile("machine.config", Drive + ":\")
    If Len(sPath) > 0 Then
         ' change the file as needed
         ProcessFile sPath
    End If

End Sub

------An extract from class zFOP.cls

Private Declare Function SearchTreeForFile Lib "ImageHlp.dll" (ByVal lpRoot As String, ByVal lpInPath As String, ByVal lpOutPath As String) As Long
Private Declare Function GetDriveType  Lib "kernel32" Alias "GetDriveTypeA" _
              (ByVal nDrive As String) As Long

Public Enum DriveTypes
End Enum

Function GetDrives(Optional DriveType As DriveTypes = DRIVE_FIXED) As Collection

' Returns a collection of local hard drives
' you can change the optional call as follows:

' Dim Drives As Collection
'Set Drives = GetDrives(DriveTypes.DRIVE_CDROM)
'Set Drives = GetDrives() ' default returns local hard disks

Dim col As Collection
Dim lc As Long
Dim lt As Long
Dim sDrive As String

Set col = New Collection

For lc = 1 To 26
    sDrive = Chr(64 + lc)
    lt = GetDriveType(sDrive + ":\")
    If lt = DriveType Then
        col.Add sDrive
    End If
Set GetDrives = col
End Function

Function LocateFile(ByVal strFilename As String, ByVal strRootPath As String) As String

' Code from Ark:
' http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20270763.html?query=search+for+files+inthedark&searchType=all

'======Input ============
'strFileName - File Name to search
'strRootPath - Path to begin search from. May be drive name
'Full path to file if success, empty string otherwithe
'Note: return first file matched

' Example:
'Private Sub Command1_Click()
'   Dim sPath As String
'   sPath = LocateFile("excell.exe","c:\")
'   If sPath = "" Then
'      MsgBox "File not found!", vbExlamation
'   Else
'      MsgBox "File full path = " & sPath , vbInformation
'   End if
'End Sub

    Dim strBuffer As String
    Dim lngResult As Long
    Const MAX_PATH = 260
    LocateFile = ""
    strBuffer = String$(MAX_PATH, 0)
    lngResult = SearchTreeForFile(strRootPath, strFilename, strBuffer)
    If lngResult <> 0 Then
        If InStr(strBuffer, vbNullChar) > 0 Then
            LocateFile = Left$(strBuffer, InStr(strBuffer, vbNullChar) - 1)
        End If
    End If
End Function


Author Comment

ID: 10811944
Thanks for the help inthedark, thats a great bit of code that will be of help in the future.

The change that we are trying to make to machine.config is a one off change, therefore calling an .exe from a system login would be a waste of resources unless we needed to check for a change every login, also the users do not log in on a regular basis, therefore using this method would result in the change taking a long time to be completed across all the machines.

I am assumning that there is no way to search HD`s and edit files of many machines on the network from ONE pc on the network (otherwise virus writers would have a field day). If this is the case can someone confirm, and I will award the points to inthedark.




Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

656 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