Hi,

I have script which working juts fine when launch it manually, also part of it works in msi but the part where he looks for "if file exist" is not working if it is in MSI. Any idea why that happening as I have no clue for that.

0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Community Chosen Answer

3

How about this one

Option Explicit

Dim objFSO, oShell, FilePath, SearchTerm, NewText, FileContent, objFile, ReplaceOldLine


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject ("WScript.Shell")


FilePath= oShell.ExpandEnvironmentStrings("%WinDir%") & "\System32\drivers\etc\hosts"

 If objFSO.FileExists(FilePath) Then

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

  Set objFile = objFSO.OpenTextFile(FilePath, 1)

  FileContent = objFile.ReadAll

   If Not Instr(FileContent,SearchTerm) = 0 then

    Set objFile = objFSO.OpenTextFile(FilePath, 2)
    ReplaceOldLine = Replace(FileContent, "# 127.0.0.1       localhost", NewText)
    objFile.WriteLine ReplaceOldLine
    objFile.Close

   End If
  objFile.Close
 
 End If

Set objFile = Nothing
Set objFSO = Nothing
Set oShell = Nothing


 

Answered 09/13/2012 by: jagadeish
Red Belt

  • see my comment on using WScript.Echo and WScript.Quit in an MSI CA. It won't work, leave the WScript. part out
  • True..
  • If you really want to show some prompt to the users.. You can use MsgBox "Hi User.. How are you"
Please log in to comment

Answers

1

Could you post your script here so we can see what it does? Where have you sequenced your script?

Answered 09/13/2012 by: henrik80
Second Degree Blue Belt

Please log in to comment
1

Here it is

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

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

If objFSO.FileExists(FilePath) Then
SearchTerm="# 127.0.0.1       localhost"
NewText=" 127.0.0.1       localhost"

Set objFile = objFSO.OpenTextFile(FilePath, 1)

FileContent = objFile.ReadAll
objFile.Close

Else
    Wscript.Echo "The file does not exist."

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

Set objFile = objFSO.OpenTextFile(FilePath, 1)

FileContent = objFile.ReadAll
objFile.Close
end If

if Instr(FileContent,SearchTerm) = 0 then

Set objFile = objFSO.OpenTextFile(FilePath, 8)

objFile.WriteLine (vbCrLf & NewText & vbCrLf)
objFile.Close

Else

ReplaceOldLine = Replace(FileContent, "# 127.0.0.1       localhost", NewText)

Set objFile = objFSO.OpenTextFile(FilePath, 2)

objFile.WriteLine ReplaceOldLine
objFile.Close

end If

WScript.Quit

Answered 09/13/2012 by: lanselots
Fourth Degree Brown Belt

Please log in to comment
1

If I read your script correctly, in the case where the file does not exist, you still do an OpenTextFile with ForReading parameter, and the default value for the create parameter, which is false.

So I think there's a problem there opening a non-existing file..

Also, I would just initialise the value of fileContent to an empty string and only open the file for reading once, if it exists, and for writing once at the end. Not open and close it as often as you do.

Answered 09/13/2012 by: pjgeutjens
Red Belt

Please log in to comment
1

What I did is not much, but if i changing something more then it is not working as expected if launching it manually. maybe someone can share there knowledge how to make it right?

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

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

If objFSO.FileExists(FilePath) Then
SearchTerm="# 127.0.0.1       localhost"
NewText=" 127.0.0.1       localhost"

Set objFile = objFSO.OpenTextFile(FilePath, 1)

FileContent = objFile.ReadAll
objFile.Close

Else
    Wscript.Echo "The file does not exist."

end if

if Instr(FileContent,SearchTerm) = 0 then

Set objFile = objFSO.OpenTextFile(FilePath, 8)

objFile.WriteLine (vbCrLf & NewText & vbCrLf)
objFile.Close

Else

ReplaceOldLine = Replace(FileContent, "# 127.0.0.1       localhost", NewText)

Set objFile = objFSO.OpenTextFile(FilePath, 2)

objFile.WriteLine ReplaceOldLine
objFile.Close

end If

WScript.Quit

Answered 09/13/2012 by: lanselots
Fourth Degree Brown Belt

Please log in to comment
1

Internet died here while I wanted to add the following to my comment:

WScript.Echo / WScript.Quit will not work in an MSI custom action, try just using Echo / Quit

Answered 09/13/2012 by: pjgeutjens
Red Belt

Please log in to comment
1

Tried removing those things, still the same. I`m using custom action and adding it as binary file. any more idea regarding this?

Answered 09/13/2012 by: lanselots
Fourth Degree Brown Belt

Please log in to comment
1

Here is what I did and it worked just fine:

 

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

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

If objFSO.FileExists(FilePath) Then
SearchTerm="# 127.0.0.1       localhost"
NewText=" 127.0.0.1       localhost"

Set objFile = objFSO.OpenTextFile(FilePath, 1)

FileContent = objFile.ReadAll
objFile.Close

Else
    msgBox "The file does not exist."

end If

if Instr(FileContent,SearchTerm) = 0 then

Set objFile = objFSO.OpenTextFile(FilePath, 8)

objFile.WriteLine (vbCrLf & NewText & vbCrLf)
objFile.Close

Else

ReplaceOldLine = Replace(FileContent, "# 127.0.0.1       localhost", NewText)

Set objFile = objFSO.OpenTextFile(FilePath, 2)

objFile.WriteLine ReplaceOldLine
objFile.Close

end If

Quit

Answered 09/13/2012 by: lanselots
Fourth Degree Brown Belt

Please log in to comment
Answer this question or Comment on this question for clarity