We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

Medium Priority
217 Views
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
    do

        line_number++

    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.

Thanks

Dan
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Is it like an INI file, is there a keyword that you can search for?
CERTIFIED EXPERT

Commented:
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)
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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
Loop
Close 1
Close 2

Author

Commented:
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:

<securityPolicy>

Thanks

Dan

CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT

Commented:
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

Commented:
Thanks loads for the help so far.

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

eg.

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.

Thanks

Dan

Author

Commented:
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:

http://www.dutchthewiz.com/vb/files/

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.

Thanks

Dan

Commented:
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.. :)~
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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.

Thanks

Dan

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.