Hi there,

I have a script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject ("WSCript.shell")
'Sets variables

if not objFSO.FileExists("%windir%") & "\system32\drivers\etc\Hosts" then
msgbox ("file not found")

end if

FilePath= oShell.ExpandEnvironmentStrings("%windir%") & "\system32\drivers\etc\Hosts"

SearchTerm="# 127.0.0.1       localhost"
NewText=" 127.0.0.1       localhost"

'Opens text file in read mode (Read=1, Write=2, Append=8)
Set objFile = objFSO.OpenTextFile(FilePath, 1)

'Adds all text in text file to "FileContent" string
FileContent = objFile.ReadAll
objFile.Close

'Instr searches "FileContent" looking for "SearchTerm", if not found returns 0
if Instr(FileContent,SearchTerm) = 0 then

'Not found code
'Opens text file in append mode so you can add text (Read=1, Write=2, Append=8)
Set objFile = objFSO.OpenTextFile(FilePath, 8)
'Writes new line to text fle vbCrLf inserts a returnto ensure new line
objFile.WriteLine (vbCrLf & NewText & vbCrLf)
objFile.Close

Else

'Found search term
ReplaceOldLine = Replace(FileContent, "# 127.0.0.1       localhost", NewText)
'Opens text file in write mode to replace old line (Read=1, Write=2, Append=8)
Set objFile = objFSO.OpenTextFile(FilePath, 2)
're-writes whole content of file with replaced values (if needed)
objFile.WriteLine ReplaceOldLine
objFile.Close

end If

WScript.Quit

works just fine but what I need is include in it error checking, for example when MSI installs it gives an error if file does not exist or is locked or other reasons, can some knowledge scripter could help in this because I have no idea how to do that.

0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Answers

0

The hosts file is there by default, unless an admin deletes it. 

beware of any additions / changes to the Hosts File can / will be removed using a System Restore point dated before the work was done with the file.
If the file is locked you may have to take ownership first before modding the file

Answered 08/23/2012 by: SMal.tmcc
Red Belt

  • if you do not care about replacing the remark'd text you can push a bat via the k1000 and just add a line at the end with

    echo 127.0.0.1 localhost >> c:\windows\system32\drivers\etc\hosts
    • This content is currently hidden from public view.
      Reason: Member has been banned from the site
      For more information, visit our FAQ's.
Please log in to comment
0

You should deploy the application when the user is logged off. In that case the file will not be locked.

Answered 08/24/2012 by: piyushnasa
Red Belt

Please log in to comment
0

Do you want to try PSFile.exe???

Answered 08/24/2012 by: jagadeish
Red Belt

Please log in to comment
This content is currently hidden from public view.
Reason: Removed by user request
For more information, visit our FAQ's.

Answer this question or Comment on this question for clarity