/build/static/layout/Breadcrumb_cap_w.png

Problems with conditions for scripted custom action in MSI

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

Answers (8)

Posted by: jagadeish 12 years ago
Red Belt
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


 


Comments:
  • see my comment on using WScript.Echo and WScript.Quit in an MSI CA. It won't work, leave the WScript. part out - pjgeutjens 12 years ago
  • True.. - jagadeish 12 years ago
  • If you really want to show some prompt to the users.. You can use MsgBox "Hi User.. How are you" - jagadeish 12 years ago
Posted by: henrik80 12 years ago
Second Degree Blue Belt
1

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

Posted by: lanselots 12 years ago
Fifth Degree Brown Belt
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

Posted by: pjgeutjens 12 years ago
Red Belt
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.

Posted by: lanselots 12 years ago
Fifth Degree Brown Belt
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

Posted by: pjgeutjens 12 years ago
Red Belt
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

Posted by: lanselots 12 years ago
Fifth Degree Brown Belt
1

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

Posted by: lanselots 12 years ago
Fifth Degree Brown Belt
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

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ