10/05/2009 2355 views
Hi all,

Currently I am trying to get to vbscripts one to amend to the Service file, but to check to see if the entry is there first. Then the other to remove the entry on an uninstall.

So far I have the following to amend to it, not sure if its the best way. But seems to work.

'On Error Resume Next
Const ForRead = 1 '*Opens file for reading
Const ForWriting = 2 '*Opens file for writing, overwriting file
Const ForAppend = 8 '*Opens file for Appending

strComputer = "." '* This computer
'* Connect to the WMI Service
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'* Open the host File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\winnt\System32\Drivers\Etc\services", ForRead)
Set objFile2 = objFSO.CreateTextFile("C:\winnt\System32\Drivers\Etc\tempservices")
Dim boolFound
boolFound = False

objFSO.Copyfile "C:\winnt\System32\Drivers\Etc\services", "C:\winnt\System32\Drivers\Etc\services.BKUP"

Do Until objFile1.AtEndOfStream
'* Read line from input file
strLine = objFile1.ReadLine
If instr(strLine, "gds_db") then
strLine=Replace("strLine, strLine, # InterBase Server")
boolFound = True
End If
objFile2.Writeline strLine

If boolFound = False then
objFile2.Writeline "gds_db 3050/tcp # InterBase Server"
End If
objFSO.Copyfile "C:\winnt\System32\Drivers\Etc\tempservices", "C:\winnt\System32\Drivers\Etc\services"

If someone could point me in the right direction that would be great, the remove of the entry is the main thing I am having a problem with.

Cheers all
0 Comments   [ + ] Show comments


Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

All Answers

You could use the script you have as a template for your removing script. Better yet, you could have one script with both functions, but I digress.

The essential difference is that you would read each line from the source file and, if the searched for text isn't found, write the line to the temporary file. If it is found, ignore that line.

BTW, in scripting as with any development, learn to be paranoid: assume nothing will work, even your object creation. The most important thing on that note here is that when you've finished writing the file, open it up again and make sure the text got entered/removed.
Answered 10/06/2009 by: VBScab
Red Belt

there have been a number of scripts posted over the past few years dealing with this task, you may want to have a look at the search.
Answered 10/06/2009 by: aogilmor
9th Degree Black Belt