cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Editing Files...

How do I insert several lines into both the "Hosts" & "Services" files without deleting anything else? I need to automate this in a package!

An exact script to do this would be great!

Thanks for your help!

Bryan
(12) Replies
here's a vbscript for this purpose. make sure that you set your custom action to run this script on install only.

Set FSO = CreateObject("Scripting.FileSystemObject")

' Open file and obtain file handle
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' Constant Value Description
' TriStateUseDefault -2 Opens the file using the system default
' TriStateTrue -1 Opens the file as Unicode
' TriStateFalse 0 Opens the file as ASCII
Const TriStateUseDefault = -2, TriStateTrue = -1, TriStateFalse = 0
Set Fout = FSO.OpenTextFile("C:\Winnt\System32\Drivers\Etc\hosts", ForAppending, False, TriStateFalse)

szLine = "193.160.184.20 swift-p1"
Fout.WriteLine(szLine)


' Close file and release file handle
Fout.Close
Nicholas,

In the line: szLine = "193.160.184.20 swift-p1" I would change everything between the quotes to the data I need including tabs, correct?

And if I needed to insert multiple lines into the same file I would have multiple " szLine = " fields?

Thanks,

Bryan
that is correct
you may use numerical values to differentiate the different lines.
eg.
szLine1 = "test1"
Fout.WriteLine(szLine1)
szLine2 = " test2"
Fout.WriteLine(szLine2)

spaces in between the quotes are also recognized
Nicholas,

Here are the contents of my .vbs script:

Set FSO = CreateObject("Scripting.FileSystemObject")

' Open file and obtain file handle Const ForReading = 1, ForWriting = 2, ForAppending = 8
' Constant Value Description
' TriStateUseDefault -2 Opens the file using the system default
' TriStateTrue -1 Opens the file as Unicode
' TriStateFalse 0 Opens the file as ASCII

Const TriStateUseDefault = -2, TriStateTrue = -1, TriStateFalse = 0
Set Fout = FSO.OpenTextFile("C:\Winnt\System32\Drivers\Etc\services", ForAppending, False, TriStateFalse)

szLine1 = "ezgwrpc 2028/tcp # EASY HRPC SYS SERVICE ON GATEWAY SYSTEM"
Fout.WriteLine(szLine1)

szLine2 = "ezsysrpc 2029/tcp # EASY HRPC SYS SERVICE ON ARCHIVE SYSTEM"
Fout.WriteLine(szLine2)

szLine3 = "ezsysdas 2031/tcp # EASY HRPC DAS SERVICE ON ARCHIVE SYSTEM"
Fout.WriteLine(szLine3)

' Close file and release file handle
Fout.Close

I get an "invalid procedure call or arguement" when I run it.

Thanks again for your help!!!

Bryan
the line
' Open file and obtain file handle Const ForReading = 1, ForWriting = 2, ForAppending = 8

should be
' Open file and obtain file handle
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Nicholas,

If I wanted to remove the entries I added to these files via the script would I need to create a new script that removed them upon uninstall?

Thanks,

Bryan
Nicholas,

From what I understand I will have to create an "Uninstallation" custom action to remove the previous changes to both the Windows "Host" & "Services" files.

Could you point to a resource that would assist me in creating a vb script that would perform this function or would you happen to have one?

I certainly owe you, let me know if there is anything I can do to assist you in the future!!!

Thanks,

Bryan
Not a problem at all, i know all of us here are more then glad to be of help. I picked up this script before but never tried it as I had other deplicate entries. In your case, I see there would be a need for it.

This script searches through the pre-determined file for a text and copies all other lines not matching that text to the temp file. At the end of the script, it deletes the original and renames the new.

good luck 🙂

Set FSO = CreateObject("Scripting.FileSystemObject")
CfgPath = Session.Property("SystemFolder") & "drivers\etc\hosts"
CfgTmp = "C:\Winnt\System32\Drivers\Etc\hosts.tmp"
Set Fin = FSO.OpenTextFile(CfgPath, 1, False, 0)
Set Fout = FSO.CreateTextFile(CfgTmp, false, false)
Do While Fin.AtEndOfStream <> True
szLine = Fin.ReadLine
' "NTSDTELCO1007A NTSDTELCO1007A #NTSDTELCO1007A" is the line added
if (szLine <> "NTSDTELCO1007A NTSDTELCO1007A #NTSDTELCO1007A") then
Fout.WriteLine szLine
end if
Loop
Fin.Close
Fout.Close

FSO.DeleteFile CfgPath, true
FSO.MoveFile CfgTmp, CfgPath

Set Fin = Nothing
Set Fout = Nothing
Set FSO = Nothing
Nicholas,

Thanks for the script! When I run it after inserting the text I want it to remove I get an error:

Line: 2
Char: 1
Error: Object required: 'Session'
Code: 800A01A8

Thanks for your help!

Bryan
Session is an object available while running the VBScript in a CA. If you attempt to run the script standalone, you will get that error.
TsungH,

Thanks for the reply. This may be a stupid question but how do I trigger the "remove" script with a custom action. It only needs to run when the software is being uninstalled. It isnt clear to me through either the custom action wizard or creating it manually as to how to do this.

Thanks guys for your help!

Bryan
You can use REMOVE property to condition the CA. Refer to msi.chm for usage, and restrictions.