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.
Answers (8)
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
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
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.
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
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