Hi, I have a script but I have no idea how to replace hard coded path in it, if I`m changing anything then it is not working anymore, can some experienced guy look in it and give me advice?:

On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Sets variables
FilePath="c:\windows\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 return(s) to 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

Answer Summary:
Option Explicit 'On Error Resume Next Dim objFSO, objWshShell, objFile Dim WinDir, FilePath, SearchTerm, NewText, FileContent, ReplaceOldLine Set objFSO = CreateObject("Scripting.FileSystemObject") Set objWshShell = CreateObject("WScript.Shell") WinDir = objWshShell.ExpandEnvironmentStrings("%WinDir%") FilePath= WinDir & "\system32\drivers\etc\hosts" SearchTerm="# 127.0.0.1 localhost" NewText=" 127.0.0.1 localhost" Set objFile = objFSO.OpenTextFile(FilePath, 1) FileContent = objFile.ReadAll objFile.Close If Instr(FileContent,SearchTerm) = 0 then Set objFile = objFSO.OpenTextFile(FilePath, 8) objFile.WriteLine (vbCrLf & NewText & vbCrLf) objFile.Close Else ReplaceOldLine = Replace(FileContent, SearchTerm, NewText) Set objFile = objFSO.OpenTextFile(FilePath, 2) objFile.WriteLine ReplaceOldLine objFile.Close End If Set objFile = Nothing Set objFSO = Nothing Set objWshShell = Nothing WScript.Quit
Cancel
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Community Chosen Answer

2

Option Explicit

'On Error Resume Next

Dim objFSO, objWshShell, objFile
Dim WinDir, FilePath, SearchTerm, NewText, FileContent, ReplaceOldLine

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


WinDir = objWshShell.ExpandEnvironmentStrings("%WinDir%")


FilePath= WinDir & "\system32\drivers\etc\hosts"

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

Set objFile = objFSO.OpenTextFile(FilePath, 1)

FileContent = objFile.ReadAll
objFile.Close

If Instr(FileContent,SearchTerm) = 0 then

   Set objFile = objFSO.OpenTextFile(FilePath, 8)
   objFile.WriteLine (vbCrLf & NewText & vbCrLf)
   objFile.Close

Else
  

   ReplaceOldLine = Replace(FileContent, SearchTerm, NewText)
   Set objFile = objFSO.OpenTextFile(FilePath, 2)
   objFile.WriteLine ReplaceOldLine
   objFile.Close

End If

Set objFile = Nothing
Set objFSO = Nothing
Set objWshShell = Nothing

WScript.Quit


 

Answered 08/15/2012 by: jagadeish
Red Belt

  • The number of space between # and 127.0.0.1 in the script should match with the number of space between # and 127.0.0.1 in your host file.. :)
  • Also.. The number of space between 127.0.0.1 and localhost in the script should match with the number of space between 127.0.0.1 and localhost in your host file.. :)
Please log in to comment

Answers

0

Thank you

Answered 08/15/2012 by: lanselots
Fourth Degree Brown Belt

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